This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes When to declare method static and when not to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When to declare method static and when not to" Watch "When to declare method static and when not to" New topic
Author

When to declare method static and when not to

John Robert
Ranch Hand

Joined: Jun 17, 2008
Posts: 39
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,
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
static methods can't access instance variables
John Robert
Ranch Hand

Joined: Jun 17, 2008
Posts: 39
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Jun 17, 2008
Posts: 39
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
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Disagree. Only make something "static" when you have a good reason to. Always try non-static first.
John Robert
Ranch Hand

Joined: Jun 17, 2008
Posts: 39
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

Joined: Oct 22, 2009
Posts: 237
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to declare method static and when not to