| Author |
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: 1506
|
|
|
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.
|
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Depends. 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
|
 |
 |
|
|
subject: Static methods Vs Instance Methods
|
|
|