• 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

Constructor Private

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have seen in one of the code snippet that the constructor of the class is made private and a public method has been provided to get the instance of the class..can someone explain to me like under what circumstances this kind of design is implemented??.
Surya
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surya,
You were probably looking at a class that represented an implementation of the singleton design pattern. Part of the definition of a singleton is that only one instance of it can be created, and code like what you described is often used to enforce that restriction. By making the constructor private, you ensure that other classes can't create multiple instances of the singleton class, and the accessor method can be used to return a reference to the one instance that is allowed to exist.
If what you're really asking is when it's appropriate to use a singleton, I'd suggest that you take a look at the "Gang of Four" Design Patterns book or one of the other titles that describes when and how to use the singleton pattern.
------------------
Brett Spell
Author, Professional Java Programming
[This message has been edited by Brett Spell (edited March 22, 2001).]
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surya,
The code snippet you are referring is an example of singleton pattern. This is done when you want to prevent more than one instance of that class to be present at a time.
For a detailed discussion look at these pages.
http://www.javaranch.com/ubb/Forum1/HTML/000617.html
http://members.tripod.com/rwald/java/articles/Singleton_in_Java.html

 
Surya Bahadur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks a lot guys...
Surya
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this link. Typesafe enum pattern also uses private constructor. A very important pattern to know about as we have to deal with types all the time.
http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic