This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static Vs Non Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Vs Non Static Methods" Watch "Static Vs Non Static Methods" New topic
Author

Static Vs Non Static Methods

Mahi Ranga
Ranch Hand

Joined: Jan 27, 2011
Posts: 35
Hi All,

When we go for Static method and When we go for Non Static method in Java.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

See this earlier post

Also, Carefully Choose One Forum


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

GENERALLY speaking...

Static methods can be used when no object needs to be created. For example, the methods of the Math class are all static. I don't need a Math object to find the cosine of pi/4, i just need to pass it the value.

a Non-static method is used when the state of an object is important. If I have a Dog class, it may have a setName() method. You can't setName if there is no Dog created. Even if there were thousands of Dog objects, you need to call setName() on a SPECIFIC dog.

So, you'd make it a non-static method of the class. The you can only call it when you have a Dog object created:

Dog myDog = new Dog();
myDog.setName("Mamie");


note: corrected mistake Campbell kindly pointed out


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
I would suggest: default to instance (not static) methods until proven otherwise.
Consider a static method when you want it to access, read, display or use nothing from the object, and you want to write alter or record nothing in the object. Consider a static method if you need to call it before any objects exist. If you are overriding or want to override or are implementing an interface, then your method must not be static.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Campbell Ritchie wrote:
fred rosenberger wrote: . . .
Dog myDog = new Dog();
myDog.setName = "Mamie";
Surely that’s… where setName() is not static.

crap...yes...
that's why i never write code first thing in the morning...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
First thing in the morning? It’s after 4pm. The only time to write code is 2am

It is a good explanation, though.
 
Don't get me started about those stupid light bulbs.
 
subject: Static Vs Non Static Methods
 
Similar Threads
Static methods
overriding static methods
accessing static method
Regarding static
Overridding