Questions tagged [static-methods]

Methods that neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance.

Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. A static method is distinguished in some programming languages with the static keyword placed somewhere in the method's signature. Static methods are called "static" because they are resolved statically (i.e. at compile time) based on the class they are called on; and not dynamically, as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. Therefore, static methods cannot be overridden.

Source: "Method (computer programming)" article on Wikipedia

2552 questions
1823
votes
10 answers

Static methods in Python?

Is it possible to have static methods in Python which I could call without initializing a class, like: ClassName.static_method()
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
1699
votes
12 answers

Meaning of @classmethod and @staticmethod for beginner?

Could someone explain to me the meaning of @classmethod and @staticmethod in python? I need to know the difference and the meaning. As far as I understand, @classmethod tells a class that it's a method which should be inherited into subclasses,…
user1632861
978
votes
22 answers

When to use static methods

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static…
KP65
  • 12,275
  • 12
  • 43
  • 46
620
votes
26 answers

Why can't static methods be abstract in Java?

The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? }
hhafez
  • 36,343
  • 33
  • 109
  • 143
554
votes
22 answers

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods? If possible, please use an example.
sgokhales
  • 49,762
  • 33
  • 123
  • 158
525
votes
24 answers

Why can't I define a static method in a Java interface?

EDIT: As of Java 8, static methods are now allowed in interfaces. Here's the example: public interface IXMLizable { static T newInstanceFromXML(Element e); Element toXMLElement(); } Of course this won't work. But why not? One of the…
cdmckay
  • 30,040
  • 21
  • 77
  • 113
387
votes
14 answers

Method can be made static, but should it?

ReSharper likes to point out multiple functions per ASP.NET page that could be made static. Does it help me if I do make them static? Should I make them static and move them to a utility class?
dlamblin
  • 40,676
  • 19
  • 92
  • 127
379
votes
10 answers

How to call getClass() from a static method in Java?

I have a class that must have some static methods. Inside these static methods I need to call the method getClass() to make the following call: public static void startMusic() { URL songPath =…
Rama
  • 4,277
  • 5
  • 19
  • 19
311
votes
8 answers

Namespace + functions versus static methods on a class

Let's say I have, or am going to write, a set of related functions. Let's say they're math-related. Organizationally, should I: Write these functions and put them in my MyMath namespace and refer to them via MyMath::XYZ() Create a class called…
RobertL
  • 3,646
  • 4
  • 17
  • 7
249
votes
13 answers

Class method differences in Python: bound, unbound and static

What is the difference between the following class methods? Is it that one is static and the other is not? class Test(object): def method_one(self): print "Called method_one" def method_two(): print "Called method_two" a_test =…
Franck Mesirard
  • 3,071
  • 3
  • 17
  • 16
212
votes
21 answers

Should private helper methods be static if they can be static

Let's say I have a class designed to be instantiated. I have several private "helper" methods inside the class that do not require access to any of the class members, and operate solely on their arguments, returning a result. public class Example { …
avalys
  • 3,502
  • 4
  • 22
  • 22
205
votes
11 answers

Static method in a generic class?

In Java, I'd like to have something as: class Clazz { static void doIt(T object) { // ... } } But I get Cannot make a static reference to the non-static type T I don't understand generics beyond the basic uses and thus can't make much…
André Chalella
  • 12,511
  • 9
  • 48
  • 60
183
votes
6 answers

Calling class staticmethod within the class body?

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…
martineau
  • 99,260
  • 22
  • 139
  • 249
178
votes
15 answers

Class with single method -- best approach?

Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches? // Initialize arguments in constructor MyClass myObject = new MyClass(arg1,…
JW.
  • 47,690
  • 30
  • 108
  • 133
153
votes
9 answers

Static extension methods in Kotlin

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…
Ragunath Jawahar
  • 18,666
  • 20
  • 102
  • 154
1
2 3
99 100