File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

constructor doubt

sthita prajna
Greenhorn

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.

Regards
sthita


[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: 281


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.

Thanks
Javin


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Sthita, Welcome to JavaRanch


Mohamed Sanaulla | My Blog
Rohini Shirpewar
Greenhorn

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.

Rohini.
sthita prajna
Greenhorn

Joined: Jan 29, 2011
Posts: 3
Hey Rohini,Javin ...thank you for your reply..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor doubt