• 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

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi alll
I read somewhere to prevent the class from instantiated
1. make class abstract
2. make all cinstructor private

The first statement is correct ,an abstract class cannot be instantiated.
I dought the second statement,it is not working.Is the statement wrong or my below program is wrong.

Output is
this is A's const.

Kindly help me to get clarified.....i am confused.
[ February 17, 2005: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about what you're doing here: you're calling a private method from a method inside the class containing that method.
That's allowed...
Try creating an instance from OUTSIDE the class, there's only 1 way to do that (and that way isn't covered by the SCJP exam).
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a matter of good form, a class with only private constructors should be declared final.

The statement "a private constructor prevents instantiation" is incorrect, particularly when being assimilated to an abstract class. A better statement might be "a private constructor prevents instantiation from outside of the enclosing class, unless it is a nested class, in which case, instantiation is prevented from anywhere outside of the enclosing top-level class (since a synthetic class is created)".
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
As a matter of good form, a class with only private constructors should be declared final.



Ramya,
I agree with Tony. An example of the above quote would be Math class which is final and has private constructor.

HTH
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all..Thanks Tony i tried instantiating outside the class An.It gives compile error..

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

Originally posted by Jeroen Wenting:
Try creating an instance from OUTSIDE the class, there's only 1 way to do that (and that way isn't covered by the SCJP exam).



Can you tell us , what is that only way ?

Thanks a lot .
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To use the similiar way that creating an object as singleton pattern that:



Nick
[ February 21, 2005: Message edited by: Nicholas Cheung ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How this is singleton . whenever we call getInstance() method , every time it will create a new object ( because you are using new in that ) .
you forgot to add static keyword anywhere .
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ February 21, 2005: Message edited by: William Goldsworthy ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


.. and even then it's only singleton across a class loader.

In any case, the use of singleton implies a design flaw.
There is always (yes, always, not almost always) a better way.
GoF set a precedent for expansion, not a religion to be followed by the blind.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic