wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructors 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 "constructors" Watch "constructors" New topic
Author

constructors

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
While i was reading about Constructors in K & B book..
in the Rules for constructors ..
I could not understand the following line.
" If the private-constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class."
Jong Limb
Greenhorn

Joined: May 08, 2004
Posts: 12
If you have a private constructor, you can't use something like:

So in order to create a new instance, the class must provide a method that will create an instance for you or a variable to a pre-created instance. For example, the following gives you a method that creates an instance:

The following provides a variable to a pre-created instance.

Note that this is not a tightly encapsulated class and is only for illustrative purposes.
[ May 10, 2004: Message edited by: Jong Limb ]
[ May 10, 2004: Message edited by: Jong Limb ]
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
I believe that it means that if you have a private constructor, for example:

That you should include a public/protected static method for getting an instance (object) of the class, ala the singleton approach:

It lets you do things like make sure only one instance of a class is instantiated at any one time, or only allow certain sources instantiate
an object, etc.
I hope this helps. If you need further reading, might I suggest the singleton tutorial over at sun:
http://java.sun.com/developer/technicalArticles/Programming/singletons/
Its written by someone with a fantastic last name, though obviously not as a cool a first name as mine
[ May 10, 2004: Message edited by: Jason Fox ]
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou John... I understood it now.
[ May 10, 2004: Message edited by: rajanisudha kudupudi ]
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou Jason...
Originally posted by Jason Fox:
I believe that it means that if you have a private constructor, for example:

That you should include a public/protected static method for getting an instance (object) of the class, ala the singleton approach:

It lets you do things like make sure only one instance of a class is instantiated at any one time, or only allow certain sources instantiate
an object, etc.
I hope this helps. If you need further reading, might I suggest the singleton tutorial over at sun:
http://java.sun.com/developer/technicalArticles/Programming/singletons/
Its written by someone with a fantastic last name, though obviously not as a cool a first name as mine
[ May 10, 2004: Message edited by: Jason Fox ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors