Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor doubt" Watch "constructor doubt" New topic

constructor doubt

sthita prajna

Joined: Jan 29, 2011
Posts: 3
Please any one give me some example for this which i have attached. i am not getting this concept.


[Thumbnail for 1.jpg]

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

The author is referring to what we make use of in singleton design pattern.

Java Experience
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 295

Hi Sthita,

If you are familier with Singleton pattern which enforces only one instance of a particular class . they have private() constructor to avoid creating object outside of a class. and they usually have a

method which returns the single available instance to the caller.

making a constructor private also prevent a class from being subclasses because subclass can not access private constructor.


SQL Query Interview Question java67 Javarevisited
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3152

Sthita, Welcome to JavaRanch

Mohamed Sanaulla | My Blog
Rohini Shirpewar

Joined: Jan 25, 2011
Posts: 15
Hi Sthita,

If for some reason you do not want your class to be instantiated by any other class by saying New MyClass(), then you can provide the getInstance method which will return the object of the class. This is done when you want to limit the instances of the class.

The example of the getInstance() method instead of the new MyClass() is a Calender class. You are not allowed to say new Calendar(), if you need a Calender instance you have to use the static method getInstace() (Calender.getInstance).

Hope this helps.

sthita prajna

Joined: Jan 29, 2011
Posts: 3
Hey Rohini,Javin ...thank you for your reply..
I agree. Here's the link:
subject: constructor doubt
It's not a secret anymore!