• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static methods Vs Instance Methods

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic