File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private constructor

 
meena latha
Ranch Hand
Posts: 219
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • 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)".
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all..Thanks Tony i tried instantiating outside the class An.It gives compile error..

Thanks
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • 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: 3830
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Bill Goldsworthy
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ February 21, 2005: Message edited by: William Goldsworthy ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic