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
92
votes
7 answers

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 out the thread safety of Web Applications. It is…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
91
votes
4 answers

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 caught my eye: A static method in Java does not…
Doval
  • 2,516
  • 1
  • 22
  • 22
86
votes
7 answers

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" static int GetInteger() { return 1; } The…
Sait
  • 19,045
  • 18
  • 72
  • 99
84
votes
4 answers

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 getlist(): l = super(Second).getlist() …
Ben J
  • 2,252
  • 4
  • 23
  • 30
84
votes
5 answers

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 Person.call_person(). But it is giving me error…
Sadiksha Gautam
  • 5,032
  • 6
  • 40
  • 71
80
votes
6 answers

How can I dynamically create class methods for a class in python

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__(self, *args, **kwargs): setattr(self, 'func',…
user1876508
  • 12,864
  • 21
  • 68
  • 105
79
votes
2 answers

static variable link error

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(Log.o) ld: symbol(s) not found for architecture…
subzero
  • 3,420
  • 5
  • 31
  • 40
74
votes
5 answers

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, this makes sense. However, the Java tutorial…
Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
72
votes
10 answers

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 class, I create an object of that class and call the…
Naveed
  • 41,517
  • 32
  • 98
  • 131
68
votes
8 answers

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
  • 105,982
  • 98
  • 297
  • 360
67
votes
2 answers

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

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 function in C++? };
Jatin
  • 1,857
  • 4
  • 24
  • 37
67
votes
5 answers

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 instantiating its class. So I tried the two…
Sanjay
  • 1,958
  • 2
  • 17
  • 25
67
votes
3 answers

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. } private aMemberFunction():void { ... …
Diosney
  • 10,520
  • 15
  • 66
  • 111
65
votes
2 answers

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, String body) { } } here is gist of the class…
Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
61
votes
7 answers

Are static methods more efficient?

In terms of memory and time, is it better to make a method static?
Yaron Naveh
  • 23,560
  • 32
  • 103
  • 158