• 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 modifier

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am just wondering, if I am not sure what modifier to use for my member functions, should I always use "public static" modifier?

I read a book, it says that it's always better to use factory method. So I set my defult constructor to private and create a public static getInstance function for creating objects. Is it true?

Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Allen Springers:
...should I always use "public static" modifier? ...


No.

For one thing, static methods cannot be overridden, so this would prevent you from using polymorphism, which is a critical feature of Object Oriented Programming.

Did this book say why it's "always better"? And does it make sense that this reason will always apply? In general, never say "always."
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In choosing between public and private (or protected or default) I recommend that any time you're not sure, use private. If you get a compile error, then you will immediately know that there's a reason why it can't be private. But in general, it's easier to change a declaration from private to public than to change it from public to private. So when in doubt, try private, and you can easily make it public later if you need to.

For static vs. non-static, it's harder to say. If you need to use any nonstatic member data, the n you can't make the method static. But if you don't need nonstatic data, it still may be beneficial to use a nonstatic method, in order to take advantage of polymorphism. Basically, if you think you might need more than one implementation of a method for different situations, then it's probably a good idea to make the method nonstatic. But if you're pretty confident that you will only ever need one implementation, then you might as well make the method static, as it will usually be easier to use.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic