• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou John... I understood it now.
[ May 10, 2004: Message edited by: rajanisudha kudupudi ]
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic