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
72
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
  • 25,780
  • 71
  • 202
  • 317
70
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,054
  • 18
  • 61
  • 99
67
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
  • 98,643
  • 98
  • 284
  • 354
67
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
  • 4,604
  • 6
  • 35
  • 67
66
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
  • 38,915
  • 31
  • 92
  • 129
62
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,663
  • 4
  • 20
  • 33
61
votes
3 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
  • 1,742
  • 2
  • 19
  • 28
60
votes
2 answers

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 my class makes extensive use of static attribute…
elite5472
  • 1,984
  • 3
  • 18
  • 26
60
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,087
  • 14
  • 62
  • 108
59
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…
Radon Rosborough
  • 3,726
  • 1
  • 30
  • 42
56
votes
6 answers

Ruby on rails - Static method

I want a method to be executed every 5 minutes, I implemented whenever for ruby (cron). But it does not work. I think my method is not accessible. The method I want to execute is located in a class. I think I have to make that method static so I…
Nostrodamus
  • 609
  • 1
  • 6
  • 8
53
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,619
  • 12
  • 39
  • 53
51
votes
1 answer

How to create a custom EL function to invoke a static method?

Im new to JSF 2. My question is related to BalusC's answer to this question jsf2 ajax update parts based on request parameters I tried the kickstart code BalusC posted and I encountered an EL parsing error: /nameofpage.xhtml @12,64…
royjavelosa
  • 2,018
  • 5
  • 29
  • 46
51
votes
11 answers

How do I know if this C# method is thread safe?

I'm working on creating a call back function for an ASP.NET cache item removal event. The documentation says I should call a method on an object or calls I know will exist (will be in scope), such as a static method, but it said I need to ensure the…
MatthewMartin
  • 29,993
  • 30
  • 102
  • 160
50
votes
7 answers

When to make a method static?

I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So let's say we have a method that does not access instance fields, do…
Dónal
  • 176,670
  • 166
  • 541
  • 787