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 ]
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).
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)".
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.
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).
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 .
[ 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
.. 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.
Tony Morris Java Q&A (FAQ, Trivia)
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, that will fit in a stocking