File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Static methods Vs Instance 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 "Static methods Vs Instance Methods" Watch "Static methods Vs Instance Methods" New topic

Static methods Vs Instance Methods

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
If a class does not have any state (that is, it consists of only methods), what is best design approch to follow?
1. Make the methods static. So that users can directly access the methods which out any objects
2. Still make it as instance methods, user should make an object and invoke the methods
Pl refer any document/url regd this design guidelines.
thank you very much
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
If you are saying that there will be no reason to create an instance of this class, then you can just make the methods static.

So much trouble in the world -- Bob Marley
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Static methods aren't polymorphic - they are resolved at compile time by the type of the reference (instead of the type of the referenced object).
Therefore, if you want to use polymorphism, you need to use instance methods - the Strategy design pattern is a good example.
OTOH, if the class provides simple helper methods (like java.lang.Math does), it's probably ok to make the methods static.
Does that help?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Static methods Vs Instance Methods
It's not a secret anymore!