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
139
votes
9 answers

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 hole in my understanding. From what I understand, a…
Carcigenicate
  • 35,934
  • 8
  • 48
  • 81
124
votes
4 answers

`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 C(object): __metaclass__ = abc.ABCMeta …
Ram Rachum
  • 71,442
  • 73
  • 210
  • 338
123
votes
3 answers

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 that multiple discrete pointer types are…
Bernie White
  • 4,120
  • 4
  • 19
  • 21
116
votes
2 answers

Static method behavior in multi-threaded environment in java

There's a simple stupid question that bother me and make several arguments in my mind. I want to throw out all the doubts about below questions. class Clstest{ public static String testStaticMethod(String inFileStr) { // section 0 …
namalfernandolk
  • 8,328
  • 11
  • 53
  • 105
115
votes
6 answers

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" self.dosomethingelse() def…
Pablo
  • 3,931
  • 12
  • 47
  • 73
109
votes
7 answers

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 the error message: Error: Cannot make a static…
Chen M
  • 1,227
  • 2
  • 11
  • 14
105
votes
15 answers

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 state C that may vary, I create a static transform.…
Lolo
  • 3,411
  • 4
  • 34
  • 45
100
votes
8 answers

.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 is known only in runtime. Thanks!
Yegor
  • 2,235
  • 2
  • 16
  • 21
97
votes
9 answers

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 { /* * Return an instance of a Model from the…
saalaa
  • 1,075
  • 1
  • 8
  • 10
96
votes
10 answers

What is the advantage of using static methods in Python?

I ran into unbound method error in python with the code import random class Sample(object): '''This class defines various methods related to the sample''' def drawSample(samplesize,List): sample=random.sample(List,samplesize) …
Curious2learn
  • 26,027
  • 37
  • 97
  • 121
94
votes
11 answers

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 "modifier" keyword static. But I don't really know what…
Panthy
  • 1,475
  • 2
  • 18
  • 26
80
votes
7 answers

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() { $result = $this->getsomthin(); }
Jom
  • 803
  • 1
  • 6
  • 4
79
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
  • 16,365
  • 16
  • 65
  • 96
77
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,266
  • 1
  • 20
  • 21
74
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,288
  • 5
  • 28
  • 40