Skip to main content

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.

Filter by
Sorted by
Tagged with
4681 votes
36 answers
1.1m views

What is the difference between @staticmethod and @classmethod in Python?

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer's user avatar
2042 votes
12 answers
1.2m views

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's user avatar
  • 326k
1940 votes
12 answers
848k views

Meaning of @classmethod and @staticmethod for beginner [duplicate]

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 ...
user avatar
1103 votes
30 answers
575k views

What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin?
pdeva's user avatar
  • 44.8k
1081 votes
26 answers
1.3m views

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 ...
KP65's user avatar
  • 13.5k
672 votes
30 answers
346k views

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 ...
hhafez's user avatar
  • 39.4k
584 votes
22 answers
295k views

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's user avatar
567 votes
24 answers
439k views

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<T> { static T newInstanceFromXML(Element e); Element toXMLElement(); } Of ...
cdmckay's user avatar
  • 32.1k
425 votes
9 answers
281k views

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's user avatar
  • 4,737
422 votes
14 answers
86k views

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's user avatar
  • 44.9k
370 votes
10 answers
97k views

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 ...
RobertL's user avatar
  • 4,256
256 votes
13 answers
163k views

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(...
Franck Mesirard's user avatar
245 votes
10 answers
189k views

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-...
zerkms's user avatar
  • 253k
229 votes
8 answers
224k views

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): @...
martineau's user avatar
  • 122k
226 votes
20 answers
94k views

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 ...
avalys's user avatar
  • 3,712
224 votes
12 answers
213k views

Static method in a generic class?

In Java, I'd like to have something as: class Clazz<T> { static void doIt(T object) { // ... } } But I get Cannot make a static reference to the non-static type T I don't understand ...
André Chalella's user avatar
204 votes
10 answers
74k views

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 ...
Ragunath Jawahar's user avatar
192 votes
5 answers
73k views

`staticmethod` and `abc.abstractmethod`: Will it blend?

In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn't work. If I do this: import abc class ...
Ram Rachum's user avatar
189 votes
15 answers
99k views

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 ...
JW.'s user avatar
  • 51.3k
174 votes
7 answers
171k views

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

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" ...
Pablo's user avatar
  • 5,047
151 votes
3 answers
83k views

Performance of static methods vs instance methods

My question is relating to the performance characteristics of static methods vs instance methods and their scalability. Assume for this scenario that all class definitions are in a single assembly and ...
Bernie White's user avatar
  • 5,015
145 votes
9 answers
10k views

Why is a static method considered a method?

I'm writing an explanation for some code for a course, and have been accidentally using the words method and function interchangeably. I decided to go back over and fix the wording, but ran into a ...
Carcigenicate's user avatar
135 votes
10 answers
77k views

What is the purpose of static methods? How do I know when to use one? [duplicate]

I ran into unbound method error in python with this code: import random class Sample(object): def drawSample(samplesize, List): sample = random.sample(List, samplesize) return ...
Curious2learn's user avatar
135 votes
4 answers
186k views

static methods and variables in Kotlin?

I want to be able to save a class instance to a private/public static variable, but I can't figure out how to do this in Kotlin. public class Foo { private static Foo instance; public Foo() { ...
Caleb Bassham's user avatar
129 votes
15 answers
48k views

Is using a lot of static methods a bad thing?

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 ...
Lolo's user avatar
  • 4,099
120 votes
2 answers
61k views

Static method behavior in multi-threaded environment in java

class Clstest{ public static String testStaticMethod(String inFileStr) { // section 0 // section 1 // do something with inFileStr ...
namalfernandolk's user avatar
115 votes
8 answers
489k views

Cannot make a static reference to the non-static method

Building a multi-language application in Java. Getting an error when inserting String value from R.string resource XML file: public static final String TTT = (String) getText(R.string.TTT); This is ...
Chen M's user avatar
  • 1,287
114 votes
10 answers
108k views

Getting the name of a child class in the parent class (static context)

I'm building an ORM library with reuse and simplicity in mind; everything goes fine except that I got stuck by a stupid inheritance limitation. Please consider the code below: class BaseModel { /*...
saalaa's user avatar
  • 1,295
113 votes
8 answers
84k views

.NET: Determine the type of “this” class in its static method

In a non-static method I could use this.GetType() and it would return the Type. How can I get the same Type in a static method? Of course, I can't just write typeof(ThisTypeName) because ThisTypeName ...
Yegor's user avatar
  • 2,564
105 votes
11 answers
258k views

Difference between Static methods and Instance methods

I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "...
Panthy's user avatar
  • 1,615
97 votes
7 answers
89k views

How to ensure thread safety of utility static method?

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 ...
Tapas Bose's user avatar
  • 29.5k
96 votes
8 answers
97k views

Using $this inside a static function fails

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context. How can I get this to work? public static function userNameAvailibility() { ...
Jom's user avatar
  • 963
93 votes
4 answers
36k views

Module function vs staticmethod vs classmethod vs no decorators: Which idiom is more pythonic?

I'm a Java developer who's toyed around with Python on and off. I recently stumbled upon this article which mentions common mistakes Java programmers make when they pick up Python. The first one ...
Doval's user avatar
  • 2,546
93 votes
3 answers
34k views

super() and @staticmethod interaction

Is super() not meant to be used with staticmethods? When I try something like class First(object): @staticmethod def getlist(): return ['first'] class Second(First): @staticmethod def ...
Ben J's user avatar
  • 2,372
87 votes
5 answers
161k views

Calling static method in python

I have a class Person and a static method in that class called call_person: class Person: def call_person(): print "hello person" In the python console I import the class Person and call ...
Sadiksha Gautam's user avatar
87 votes
7 answers
86k views

Static function declared but not defined in C++

I'm getting an error from the following code using C++. Main.cpp #include "file.h" int main() { int k = GetInteger(); return 0; } File.h static int GetInteger(); File.cpp #include "file.h"...
Sait's user avatar
  • 19.5k
85 votes
6 answers
75k views

How can I dynamically create class methods for a class in python [duplicate]

If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(...
user1876508's user avatar
  • 13.1k
79 votes
2 answers
47k views

static variable link error [duplicate]

I'm writing C++ code on a mac. Why do I get this error when compiling?: Undefined symbols for architecture i386: "Log::theString", referenced from: Log::method(std::string) in libTest.a(...
subzero's user avatar
  • 3,450
75 votes
5 answers
19k views

Why are class static methods inherited but not interface static methods?

I understand that in Java static methods are inherited just like instance methods, with the difference that when they are redeclared, the parent implementations are hidden rather than overridden. Fine,...
Resigned June 2023's user avatar
72 votes
10 answers
67k views

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

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 ...
Naveed's user avatar
  • 41.9k
70 votes
5 answers
20k views

Is @staticmethod decorator needed for declaring a Static Method in Python?

I am curious about why we need the @staticmethod decorator to declare method as static. I was reading about static methods in Python, and I came to know that a static method can be callable without ...
Sanjay's user avatar
  • 1,988
69 votes
2 answers
81k views

Can we have a static virtual functions? If not, then WHY? [duplicate]

Possible Duplicate: C++ static virtual members? Can we have a static virtual functions? If not, then WHY? class X { public: virtual static void fun(){} // Why we cant have static virtual ...
Jatin's user avatar
  • 1,877
68 votes
8 answers
10k views

ReSharper complains when method can be static, but isn't

Why does ReSharper complain when a method can become static, but is not? Is it because only one instance of a static method is created (on the type) and thus save on performance?
Andreas Grech's user avatar
68 votes
3 answers
124k views

TypeScript: Access static methods within classes (the same or another ones)

Suppose we have the following code: [Test.js file]: class Test { ... public static aStaticFunction():void { ... this.aMemberFunction(); // <- Issue #1. } ...
Diosney's user avatar
  • 10.6k
67 votes
2 answers
176k views

How to verify static void method has been called with power mockito

I am using the following. Powermock-mockito 1.5.12 Mockito 1.95 junit 4.11 Here is my utils class public void InternalUtils { public static void sendEmail(String from, String[] to, String msg, ...
Chun ping Wang's user avatar
63 votes
7 answers
20k views

Are static methods more efficient?

In terms of memory and time, is it better to make a method static?
Yaron Naveh's user avatar
  • 24.1k
63 votes
5 answers
60k views

What is the use of private static member functions?

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 ...
rve's user avatar
  • 6,027
62 votes
1 answer
130k views

How do I call a static method of another class

I have a class, lets say CAppPath which has a static method: public: static CString GetAppPath(); and in CAppPath.cpp it's defined as: CString CAppPath::GetAppPath() { return "C:\..\MypAth"...
Simsons's user avatar
  • 12.6k
60 votes
2 answers
29k views

PHP Can static:: replace self::?

I am a little confused with this matter. I am designing an ORM class that tries to behave very similarly to ActiveRecord in ruby on rails, but that's beside the point. What I'm trying to say is that ...
elite5472's user avatar
  • 2,184
59 votes
3 answers
94k views

Can a static method be overridden in C#?

I was told that static methods are implicitly final and therefore can't be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just ...
aspiring's user avatar
  • 1,597

1
2 3 4 5
57