static-methods

Swift calling static methods: type(of: self) vs explicit class name

浪子不回头ぞ 提交于 2019-12-03 01:14:13
In swift, an instance func can't call a static/class func without prefixing the method call with the class name. OR you can use type(of: self) , e.g class Foo { static func doIt() { } func callIt() { Foo.doIt() // This works type(of: self).doIt() // Or this doIt() // This doesn't compile (unresolved identifier) } } My question is, what's the difference here? Is it just a matter of coding style, or is there some difference e.g. static or dynamic dispatch going on? If it is just coding style, what's the preferred style? Hamish There are two main differences. 1. The value of self inside the

Why use static method in PHP's laravel model class?

人走茶凉 提交于 2019-12-02 23:59:22
In PHP laravel, we have codes like $user = User::find(1); var_dump($user->name); I am not concerning how to use the find method, I am concerning why laravel use a static method? Shouldn't the use of static method make the method hard to test? Will it be better if they designed using singleton? e.g. $user = User::getInstance()->find(1); var_dump($user->name); In fact, your example is very similar to what Laravel does behind the scene. When you do User::find() , you are actually asking for a new instance, either an instance of Collection or a QueryBuilder. Illuminate\Database\Eloquent\Model (

Realistic use case for static factory method?

杀马特。学长 韩版系。学妹 提交于 2019-12-02 22:10:05
I'm familiar with the idea and benefits of a static factory method, as described in Joshua Bloch's Effective Java : Factory methods have names, so you can have more than one factory method with the same signature, unlike constructors. Factory methods don't have to create a new object; they can return a previously-created object. This is good for immutable objects or value objects. Factory methods can return an object of any subtype of their return type, unlike constructors. Now I'm trying to explain static factory methods for someone who is learning Java and OO principles. She learns best from

public static void main () access non static variable

萝らか妹 提交于 2019-12-02 19:33:50
Its said that non-static variables cannot be used in a static method.But public static void main does.How is that? No, it doesn't. public class A { int a = 2; public static void main(String[] args) { System.out.println(a); // won't compile!! } } but public class A { static int a = 2; public static void main(String[] args) { System.out.println(a); // this works! } } or if you instantiate A public class A { int a = 2; public static void main(String[] args) { A myA = new A(); System.out.println(myA.a); // this works too! } } Also public class A { public static void main(String[] args) { int a = 2

What is the use of private static member functions?

。_饼干妹妹 提交于 2019-12-02 18:46:00
I was looking at the request parser from the boost::asio example and I was wondering why the private member functions like is_char() are static ? : class request_parser { ... private: static bool is_char(int c); ... }; It is used in the function consume which is not a static function: boost::tribool request_parser::consume(request& req, char input) { switch (state_) { case method_start: if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } ... Only member functions can call is_char() and no static member function is calling is_char() . So is there a reason why these

Are static methods a DI anti-pattern?

匆匆过客 提交于 2019-12-02 17:05:32
I am a Java developer who is beginning to grasp the full power of dependency injections, and it suddenly dawned on me that there's no way to inject a static method. So it got me thinking: are static methods DI anti-patterns? More importantly: if I were to embrace dependency injection, does this mean I need to stop coding static methods? I ask because there is no way to mock them and inject mock statics during unit tests, which is a huge turn-off for me. Edit : I know that a common way to "wrap" and inject an existing static method is like this: public class Foo { public static void bar() { ...

Static methods - How to call a method from another method?

做~自己de王妃 提交于 2019-12-02 14:48:02
When I have regular methods for calling another method in a class, I have to do this class test: def __init__(self): pass def dosomething(self): print "do something" self.dosomethingelse() def dosomethingelse(self): print "do something else" but when I have static methods I can't write self.dosomethingelse() because there is no instance. How do I have to do in Python for calling an static method from another static method of the same class? Edit: what a mess. Ok, I edited back the question to the original question. I already have the answer to the second question that's in Peter Hansen's

When to make a method static?

柔情痞子 提交于 2019-12-02 14:14:26
I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So let's say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (without a reference to an instance). Perhaps another way of asking the same question is whether you use static or non-static as the default? I use static methods whenever I can. Advantages: When calling a static method from inside an instance method, you can be sure

how to call static method inside non static method in c#

こ雲淡風輕ζ 提交于 2019-12-02 13:59:36
问题 how to call static method inside non static method in c# ? Interviewer gave me scenario : class class1 { public static void method1(){} public void method2() { //call method1() } How can we do it 回答1: A normal practice is to call static method with class name. See: Static Classes and Static Class Members (C# Programming Guide) The static member is always accessed by the class name , not the instance name. Only one copy of a static member exists, regardless of how many instances of the class

non-static method getActivity()

那年仲夏 提交于 2019-12-02 13:07:19
I made the method "showResult" static but i have problem with : "Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();" How can resolve my problem? I will share my code. THANKS IN ADVANCE EVERYBODY! Here is my first class Fragment.java public class MyListFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener { ListView lv; ArrayList<Planet> planetList; static PlanetAdapter plAdapter; BirraAdapter biAdapter; PlanetAdapter.PlanetHolder holder; @Override public View onCreateView