• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to declare method static and when not to

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers,
I know a static method is a part of a class as opposed to the object. But my dilemma is when i put a method as part of a class, when should i declare it as static, and when should i declare it non static. Here is my theory please correct me if i am wrong, if the method is accessing any of the instance variables(which effect the state of the object) , then declare method non static. Else declare static. Am I correct?

Any response would be appreciated..

Thanks,
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods can't access instance variables
 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:static methods can't access instance variables



Thanks for the response, my question was, is that the criteria to determine if a method should be declared static or not.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Not needing access to static data is one criterion for allowing a method to be static. I'd add three more requirements even given thids first criterion is met.

1) Methods that could possibly need to vary according to the class in which they're defined should not be static. A static method isn't polymorphic -- i.e., you can't override it. Therefore, only make a method static if you're sure that every subclass of the method's class will want the same definition of that method.

2) Methods that might need to be replaced or stubbed out during testing. If a class has a method called "queryDatabase()", during testing you might want to write a subclass that overrides queryDatabase() to just return some canned data. If the method is static, you can't do that. If you're new to programming, you might not appreciate this last point, but it's very important.

3) Limit the number of classes with widely-used static methods. For one class to use a static method of a second class, the first class must "hard-code" the name of the second class, and to compile the first class, the second one will be required. This kind of dependency, multiplied many times, leads to complex, interconnected systems that take a long time to compile and are very hard to understand. Much better to define widely-used methods in an interface (as non-static methods) and then use private implementation classes; coupling can be greatly reduced this way.

So actually, that leaves only a few kinds of methods that really should be static. The methods in java.lang.Math that return the square root, absolute value, etc, are great examples. They're all concentrated in one class, and you'd never want to change their behavior. The default should always be instance methods; static methods are a fairly rare exception.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Robert wrote:... my question was, is that the criteria to determine if a method should be declared static or not.


There are several places "static" is used. You want to provide some methods (utility methods) which the clients need not to have a instance of your class (that is those do not depend on state of the instance) to invoke. And some design patterns (Factory pattern) utilize "static" very much.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:static methods can't access instance variables

, hence if your method doesn't need to manipulate instance variables it can be static.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Robert wrote:Ranchers,
I know a static method is a part of a class as opposed to the object. But my dilemma is when i put a method as part of a class, when should i declare it as static, and when should i declare it non static. Here is my theory please correct me if i am wrong, if the method is accessing any of the instance variables(which effect the state of the object) , then declare method non static. Else declare static. Am I correct?



A good rule of thumb is that you make the method non-static as a first approximation. You'll quickly discover whether this was the wrong choise becasue then you cannot use the method the way you intended. Then you change to static.

But in principle you use a static method for stuff that's of concerns to all objects of the class and not individual objects. This is the same as to say that if the method concerns the class as a whole rather than individual objects then the method should be static. One example could be a method that creates objects of the class. It should be static. Another example could be a method which returns the number of created objects. It should be static.

But as I said, make non-static your default.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Hi John,

Not needing access to static data is one criterion for allowing a method to be static. . . .

That looks like a spelling error, Ernest. Surely it's "access to instance data"?
 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you all , the inputs gave me a great insight, especially the response by Ernest Friedman-Hill. I never thought in those lines before.

uj nossnahoj wrote:
A good rule of thumb is that you make the method non-static as a first approximation. You'll quickly discover whether this was the wrong choice because then you cannot use the method the way you intended. Then you change to static.



I actually was thinking the other way around, my theory was, first try to see if static woks for a method, if it does then do static, because from a efficiency/memory perspective you will not need to instantiate the class to call this method. May be I am wrong, I am not sure.

Once again thank you all.. Have a great weekend..
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree. Only make something "static" when you have a good reason to. Always try non-static first.
 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Disagree. Only make something "static" when you have a good reason to. Always try non-static first.



Ya you are right. What you said even is in line with

Ernest Friedman-Hill wrote:Hi John,

Not needing access to static data is one criterion for allowing a method to be static. I'd add three more requirements even given thids first criterion is met.


2) Methods that might need to be replaced or stubbed out during testing. If a class has a method called "queryDatabase()", during testing you might want to write a subclass that overrides queryDatabase() to just return some canned data. If the method is static, you can't do that. If you're new to programming, you might not appreciate this last point, but it's very important.

So actually, that leaves only a few kinds of methods that really should be static. The methods in java.lang.Math that return the square root, absolute value, etc, are great examples. They're all concentrated in one class, and you'd never want to change their behavior. The default should always be instance methods; static methods are a fairly rare exception.



I will follow that in my design. Thank you.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Robert wrote:I actually was thinking the other way around, my theory was, first try to see if static woks for a method, if it does then do static, because from a efficiency/memory perspective you will not need to instantiate the class to call this method. May be I am wrong, I am not sure.


If you start by making a method non-static but realize that you then unnecessarily have to create an object to use the method, then this is a clear sign you made the wrong choise, so you change to static. See, the rule of thumb works.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic