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

2793 questions
4574
votes
36 answers

@staticmethod vs @classmethod in Python

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
2009
votes
12 answers

Static methods in Python?

Can I define a static method which I can call directly on the class instance? e.g., MyClass.the_static_method()
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1886
votes
12 answers

Meaning of @classmethod and @staticmethod for beginner

What do @classmethod and @staticmethod mean in Python, and how are they different? When should I use them, why should I use them, and how should I use them? As far as I understand, @classmethod tells a class that it's a method which should be…
user1632861
1066
votes
24 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
  • 13,315
  • 13
  • 45
  • 46
661
votes
30 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
  • 38,949
  • 39
  • 113
  • 143
581
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.
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
560
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
  • 31,832
  • 25
  • 83
  • 114
421
votes
9 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,697
  • 5
  • 22
  • 19
415
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
  • 43,965
  • 20
  • 101
  • 140
355
votes
9 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
  • 4,106
  • 4
  • 20
  • 7
254
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,169
  • 3
  • 20
  • 17
233
votes
10 answers

Why does PyCharm propose to change method to static?

The new pycharm release (3.1.3 community edition) proposes to convert the methods that don't work with the current object's state to static. What is the practical reason for that? Some kind of micro-performance(-or-memory)-optimization?
zerkms
  • 249,484
  • 69
  • 436
  • 539
224
votes
20 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,662
  • 4
  • 25
  • 22
224
votes
7 answers

Python version <= 3.9: 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
  • 119,623
  • 25
  • 170
  • 301
218
votes
12 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
  • 13,788
  • 10
  • 54
  • 62
1
2 3
99 100