static-methods

Java method call chaining in static context

旧巷老猫 提交于 2019-12-07 01:25:46
问题 In StringBuilder class I can do like this: StringBuilder sb = new StringBuilder(); sb.append( "asd").append(34); method append returns StringBuilder instance, and I can continuosly call that. My question is it possible to do so in static method context? without class instance 回答1: Yes. Like this (untested). public class Static { private final static Static INSTANCE = new Static(); public static Static doStuff(...) { ...; return INSTANCE; } public static Static doOtherStuff() { .... return

implement a virtual method from the base class as derived in static

感情迁移 提交于 2019-12-06 16:16:11
I have an abstract base class with a virtual method. In the derived class, this method is implemented. However, I want the function in the derived class as a static method in order to be able to call the function without instantiating an object of that class. class Base { virtual double Foo(double rParam) const; }; class Derived1 : public Base { static double Foo(double rParam); }; class Derived2 : public Base { static double Foo(double rParam); }; Essentially, Derived1 and Derived2 provide different implementations of a static function (that do not depend on object data), but I want those

why this weird order of constructor/static initializer/static member function in java?

半腔热情 提交于 2019-12-06 11:42:31
public class DataFactory { private static DataFactory ourInstance = new DataFactory(); static { System.out.println("static initialize"); } private DataFactory() { System.out.println("constructor"); } public static void doNothing() { System.out.println("inside doNothing"); } } public class App { public static void main(String[] args) { System.out.println("main start"); DataFactory.doNothing(); } And After I run it, here is the printed sequence: main start constructor static initialize inside doNothing Why calling DataFactory.doNothing() will trigger Constructor? and why constructor is running

Is using static private methods really faster/better than instance private methods?

核能气质少年 提交于 2019-12-06 10:27:47
问题 What I'm asking is whether there is a difference between doing this: public Something importantBlMethod(SomethingElse arg) { if (convenienceCheckMethod(arg)) { // do important BL stuff } } private boolean convenienceCheckMethod(SomethingElse arg) { // validate something } And this: public Something importantBlMethod(SomethingElse arg) { if (convenienceCheckMethod(arg)) { // do important BL stuff } } private static boolean convenienceCheckMethod(SomethingElse arg) { // validate something } I

static method as default parameter to a class method

荒凉一梦 提交于 2019-12-06 06:06:07
问题 My question is about two answers to another question: Using class/static methods as default parameter values within methods of the same class. I am trying to understand if there's really a difference between what the two answers do, and if so, what's the pros and cons of each. Question: how to use a class method as a default parameter to a method in the same class. Answer 1: use a function instead of a class method class X: def default_func(x): return True def test(self, func = default_func):

Avoiding “Access to a static member of a type via a derived type”

霸气de小男生 提交于 2019-12-06 04:38:21
I believe this is purely a Resharper warning, but the reasoning behind it (explained here ) makes good sense. What Greg Beech is saying is that you can call a base-class static method from a sibling class... in his example he uses: var request = (FtpWebRequest)HttpWebRequest.Create(...) ... which is misleading. So is there a design that would allow me to avoid this warning in the following classes? public abstract class BaseLog { // I omitted several other properties for clarity protected static string category; protected static TraceEventType severity; static BaseLog() { category = "General";

Python: Assigning staticmethod to class variable gives error

∥☆過路亽.° 提交于 2019-12-06 03:55:01
I want to assign a static method to a class variable in Python, and below is what my code looks like. class Klass: classVariable = None @staticmethod def method(): print "method called" Klass.classVariable = Klass.method Klass.method() Klass.classVariable() This gave me an error at the last line, TypeError: unbound method method() must be called with Klass instance as first argument (got nothing instead). But when I change the static method to class method it works. Can anyone give me any idea of why this is the case? Backstory (descriptor protocol) First, we need to know a little about python

how to call non static method from static method in android

六眼飞鱼酱① 提交于 2019-12-06 01:34:34
I am facing a big problem in calling non static method from static method. This is my code Class SMS { public static void First_function() { SMS sms = new SMS(); sms.Second_function(); } public void Second_function() { Toast.makeText(getApplicationContext(),"Hello",1).show(); // This i anable to display and cause crash CallingCustomBaseAdapters(); //this was the adapter class and i anable to call this also } I am able to call Second_function but unable to get Toast and CallCustomBaseAdapter() method, crash occurs. What should I do to fix that issue ? public static void First_function(Context

How compatible are static class methods and regular routine pointers?

会有一股神秘感。 提交于 2019-12-05 22:09:38
It seems to me that static class methods and regular routine pointers are compatible from a practical viewpoint but the compiler doesn't know this. Example: type TFunc = function(i: Integer): string; TMyClass = class public class function StaticMethod(i: Integer): string; static; end; class function TMyClass.StaticMethod(i: Integer): string; begin Result := '>' + IntToStr(i) + '<'; end; function GlobalFunc(i: Integer): string; begin Result := '{' + IntToStr(i) + '}'; end; procedure CallIt(func: TFunc); begin Writeln(func(42)); end; begin CallIt(TMyClass.StaticMethod); // 1a: doesn't compile

Tips for avoiding Static Method Overuse

偶尔善良 提交于 2019-12-05 20:27:26
问题 I'm refactoring some code and I'm looking at a class called HFile. HFile has all private constructors so that you can't actually create instances of it. Instead of creating instances of HFiles as follow: var file = new HFile('filename') file.Save() all HFile interaction is handled via static methods. So if I wanted to save a file I would call: HFile.save('filename') and then internally an instance of HFile would be created and then saved. Obviously without knowing the whole story any reader