• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

private constructor

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is wrt to Kathy Sierra & Bert Bates book. In chapter 5, page No. 25, it says:
"Constructors can use any access modifier, including private. (A private
constructor means only code within the class itself can instantiate an object of that type, so 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.)"

Well, I understood that point except the last part:
".................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."

can anyone explain this?

Thanks,
Lohit
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Use the above like:



Does the above help? The makeA method can do things like ensure only one A object is ever created, or get an A object out of a ready constructed cache of precreated A objects.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a typical example which i guess would make things easier to understand.



The Logger class has just one constructor and that too is private. So, an instance of this class can be created only within this class. Now for the outside world to use this Logger object, the Logger class has to provide either a static method which returns an instance or a static variable which holds a reference to the instance.

Hope it helped.

Regards,
Arvind
 
Lohit Hosur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, now im clear.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does declaring a static method as final make sense???
Ex:
static final void m1(){}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-----------------------
Does declaring a static method as final make sense???
Ex:
static final void m1(){}
-----------------------

static allows you to access the method using the class name ( ie without instantiating the object ), as the constructor is private. Mostly used for factory patterns and not exposing the object creation logic.

"final" does not allow you to override the same method in the subclasses.

So you have to decide while designing your classes/Systems what exactly needed.

Typical example is Singleton Design Pattern.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u want to make the instance of the class (which has private constructor) in some other class u can make it through static method of that class which has access to the private member of class & can be called without object of that class.

With Best Regards!
Sachin.
 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic