File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private constructor" Watch "private constructor" New topic
Author

private constructor

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
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

Joined: Oct 12, 2000
Posts: 5093
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).


42
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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)".


Tony Morris
Java Q&A (FAQ, Trivia)
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Thanks to all..Thanks Tony i tried instantiating outside the class An.It gives compile error..

Thanks
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Nov 07, 2003
Posts: 4982
To use the similiar way that creating an object as singleton pattern that:



Nick
[ February 21, 2005: Message edited by: Nicholas Cheung ]

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 21, 2004
Posts: 27

[ February 21, 2005: Message edited by: William Goldsworthy ]

Increasingly, people seem to misinterpret complexity as sophistication, which is baffling - the incomprehensible should cause suspicion rather than admiration. Possibly this trend results from a mistaken belief that using a somewhat mysterious device confers an aura of power on the user. Niklaus Wirth
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


.. 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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: private constructor