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
193
votes
10 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
  • 19,513
  • 22
  • 110
  • 155
187
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.
  • 50,691
  • 36
  • 115
  • 143
180
votes
5 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 …
159
votes
7 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
  • 4,821
  • 12
  • 52
  • 82
146
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,780
  • 4
  • 23
  • 21
144
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
  • 43,494
  • 9
  • 68
  • 117
132
votes
4 answers

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() { if (instance == null) { …
Caleb Bassham
  • 1,874
  • 2
  • 16
  • 33
126
votes
10 answers

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

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
  • 31,692
  • 43
  • 108
  • 125
124
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,935
  • 5
  • 40
  • 50
119
votes
2 answers

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 // section 2 // section 3 …
ironwood
  • 8,936
  • 15
  • 65
  • 114
114
votes
8 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,277
  • 2
  • 11
  • 14
113
votes
10 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,255
  • 2
  • 9
  • 11
111
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,514
  • 2
  • 19
  • 27
104
votes
10 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,605
  • 4
  • 19
  • 27
96
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
  • 963
  • 1
  • 6
  • 4