Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor doubt

 
sthita prajna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please any one give me some example for this which i have attached. i am not getting this concept.

Regards
sthita
1.jpg
[Thumbnail for 1.jpg]
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The author is referring to what we make use of in singleton design pattern.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sthita, Welcome to JavaRanch
 
Rohini Shirpewar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rohini,Javin ...thank you for your reply..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic