• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

constructors

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]

 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic