aspose file tools*
The moose likes Java in General and the fly likes When do i go for static methods and when i go for non static methods ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When do i go for static methods and when i go for non static methods ?" Watch "When do i go for static methods and when i go for non static methods ?" New topic
Author

When do i go for static methods and when i go for non static methods ?

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
hi helpers,

i am having 2 years of experience in java, but still i don't know When do i go for static methods and when i go for non static methods ?

please help me out.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Hi, I think that the methods which do not depend on and change the state of an object should be made static.


Piyush
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
When I want a method to load at application startup .e.g. creating connection instances to db, I write it in static method. I think static method is loaded when a first reference to the class is made - some one correct if I am wrong
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

static methods cant work with instance variables of the class, they can only work with static variables.

so for starters, if your method is not providing instance specific behavior you could make it static.

Class - TennisPlayer
Method - getAnnualIncome() - This method needs to be an instance method as you need to create an instance first like Roger Federer and then call the getAnnualIncome() method. If you make this method static (class level), this method wouldn't know which instances (Federer's or Nadal's) income should it return, and moreover, static methods could be invoked without any instances created as well.

but if you have a method of a class not bound to any instance, go ahead and make it static
Method - static String categorizePlayer(int ranking){
if(ranking < 20){
return "champ"
}else{
return "needs practice"
}
}

So generally, utility methods are static (working only on the passed parameters), also in Singletons getInstance method is static, and methods plainly working with static variables could also be static.


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

I think static method is loaded when a first reference to the class is made


static blocks are executed when the class is loaded, static methods wont run until somebody invokes them.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
i did not get you people explained all above.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Static methods cannot access non-static member variables or methods. So if your method needs to do that, then it cannot be static. If it doesn't, then it can be static. And if it can be static, you might as well make it static.

Another way of looking at it is - is the operation specific to that particular instance? If it uses or modified data belonging to that instance, then it can't be static.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Great explanation Matthew . static is bounded to a class , not an Object. If a method does not take any information from a instance/field, similarly does not put any information into a instance/field of a class/object can be marked as static.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
joy b chakravarty wrote: . . .
but if you have a method of a class not bound to any instance, go ahead and make it static
Method - static String categorizePlayer(int ranking){
if(ranking < 20){
return "champ"
}else{
return "needs practice"
}
} . . .
Not a good example, I am afraid; you need to know a certain player's ranking, which would be an instance field.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When do i go for static methods and when i go for non static methods ?