static-methods

How do I make my scala method static?

落花浮王杯 提交于 2019-11-26 23:15:46
问题 I have a class class MyClass { def apply(myRDD: RDD[String]) { val rdd2 = myRDD.map(myString => { // do String manipulation } } } object MyClass { } Since I have a block of code performing one task (the area that says "do String manipulation" ), I thought I should break it out into it's own method. Since the method is not changing the state of the class, I thought I should make it a static method. How do I do that? I thought that you can just pop a method inside the companion object and it

Python decorator as a staticmethod

拟墨画扇 提交于 2019-11-26 22:20:37
I'm trying to write a python class which uses a decorator function that needs information of the instance state. This is working as intended, but if I explicitly make the decorator a staticmetod, I get the following error: Traceback (most recent call last): File "tford.py", line 1, in <module> class TFord(object): File "tford.py", line 14, in TFord @ensure_black TypeError: 'staticmethod' object is not callable Why? Here is the code: class TFord(object): def __init__(self, color): self.color = color @staticmethod def ensure_black(func): def _aux(self, *args, **kwargs): if self.color == 'black':

Calling a Activity method from BroadcastReceiver in Android

主宰稳场 提交于 2019-11-26 19:57:52
问题 Here I am creating an online application that depends only on Internet. So whenever there is a network error it must notify user. For that, I have created a BroadcastReciver that receives call when network connection gets lost(Internet). All this works perfectly. Now what I need is that I have to call a method of Activity from this Broadcast Receiver, where I have created an Alert Dialogue. I have read many answers on stack-overflow.com that I can declare that method static and call by using

Static extension methods in Kotlin

点点圈 提交于 2019-11-26 19:55:23
问题 How do you define a static extension method in Kotlin? Is this even possible? I currently have an extension method as shown below. public fun Uber.doMagic(context: Context) { // ... } The above extension can be invoked on an instance. uberInstance.doMagic(context) // Instance method but how do I make it static method like shown below. Uber.doMagic(context) // Static or class method 回答1: To achieve Uber.doMagic(context) , you can write an extension to the companion object of Uber (the

Calling class staticmethod within the class body?

跟風遠走 提交于 2019-11-26 19:54:28
When I attempt to use a static method from within the body of the class, and define the static method using the built-in staticmethod function as a decorator, like this: class Klass(object): @staticmethod # use as decorator def _stat_func(): return 42 _ANS = _stat_func() # call the staticmethod def method(self): ret = Klass._stat_func() + Klass._ANS return ret I get the following error: Traceback (most recent call last):<br> File "call_staticmethod.py", line 1, in <module> class Klass(object): File "call_staticmethod.py", line 7, in Klass _ANS = _stat_func() TypeError: 'staticmethod' object is

When NOT to use the static keyword in Java?

不羁的心 提交于 2019-11-26 19:46:45
When is it considered poor practice to use the static keyword in Java on method signatures? If a method performs a function based upon some arguments, and does not require access to fields that are not static, then wouldn't you always want these types of methods to be static? One reason why you may not want it to be static is to allow it to be overridden in a subclass. In other words, the behaviour may not depend on the data within the object, but on the exact type of the object. For example, you might have a general collection type, with an isReadOnly property which would return false in

When should I use static methods in a class and what are the benefits?

一个人想着一个人 提交于 2019-11-26 19:41:32
I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a class, I create an object of that class and call the desired method. Q: Static variable in a method holds it's value even when method is executed but accessible only in its containing method but what is the best definition of static method? Q: Is calling the static method without creating object of that class is the only benefit of static method? Q: What is the accessible range for static method?

Is using a lot of static methods a bad thing?

寵の児 提交于 2019-11-26 19:31:55
I tend to declare as static all the methods in a class when that class doesn't require to keep track of internal states. For example, if I need to transform A into B and don't rely on some internal state C that may vary, I create a static transform. If there is an internal state C that I want to be able to adjust, then I add a constructor to set C and don't use a static transform. I read various recommendations (including on StackOverflow) NOT to overuse static methods but I still fail to understand what it wrong with the rule of thumb above. Is that a reasonable approach or not? John Millikin

How to ensure thread safety of utility static method?

好久不见. 提交于 2019-11-26 18:55:28
问题 Is there any general way or rules exits by which we can ensure the thread safety of static methods specifically used in various Utility classes of any applications. Here I want to specifically point out the thread safety of Web Applications. It is well know that static methods with Immutable Objects as parameters are thread safe and Mutable Objects are not. If I have a utility method for some manipulation of java.util.Date and that method accepts an instance of java.util.Date , then this

C# Static variables - scope and persistence

吃可爱长大的小学妹 提交于 2019-11-26 18:54:51
I just did a little experiment: public abstract class MyClass { private static int myInt = 0; public static int Foo() { return myInt; } public static int Foo(int n) { myInt = n; return bar(); } private static int bar() { return myInt; } } and then I ran: MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(3).ToString()); MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(10).ToString()); MessageBox.Show(MyClass.Foo().ToString()); The results I expected were 0, 3, 0, 10, 0. To my surprise, I got 0, 3, 3, 10, 10. How long do these changes persist for? The