This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi Vineela, No, it is not a requirement to declare it final. These constructions are used for example when you want to control the amount of times you create an object of that class. By instantiating a class through a static method that calls one of these private constructors you could increment a private counter variable that will tell you how many times the class has been instantiated. No other class can instantiate this class so the counting goes fine. HTH, best regards, Gian Franco HTH, best regards, Gian Franco
Originally posted by Anupam Sinha: A class with private constructors can be instantiated but a final class can't be.
WHAT?!? No, no, no. A final class can't be extended, but it can certainly be instantiated. Check out the JLS, §126.96.36.199 final Classes:
A class can be declared final if its definition is complete and no subclasses are desired or required. A compile-time error occurs if the name of a final class appears in the extends clause (�8.1.3) of another class declaration; this implies that a final class cannot have any subclasses.
In addition, there is no requirement that a class that has all private constructors be considered final. It would seem, at first glance, that a class with all private constructors would be "implicitly" final, but that's not actually the case. There is at least one case that I know of in which you could extend such a class - can you think of what it is? I hope that helps, Corey
Just to make explicit the issue y'all are hinting at... if the constructors are all private, then the class is *essentially* final, because a subclass has to be able to invoke the superclass constructor, and if all constructors are private, the poor subclass can't invoke them. So if you make all constructors private, you've made the class *virtually* final... except... (I'll give one more hint for what I assume Corey is referring to) there IS a scenario (and this is kind of on the exam) in which access modifiers like *private* can be ignored. So I think Corey's question is: what kind of class is allowed to invoke the private members of another class? Because if you can answer that, then you can see where you could have a subclass of a non-final class, EVEN though all the constructors of the superclass are private. Because there IS a type of class that can *ignore* the private modifiers of its superclass. There are very few (if any) good reasons for which you'd consider this as a particular design choice, but it is definitely supported by the language (and you might have a scenario where it matters for you) and in THAT case, you do need to know about this issue for the exam. Good question! (and good hint, Corey ) cheers, Kathy
Joined: Apr 13, 2003
With my limited brain the only answer that comes to my mind is Inner classes. Is that the answer?
what kind of class is allowed to invoke the private members of another class
The only thing I can think of is an inner class....and if it provided a "factory" method to create the outer class.... For example:
Running this results in: inside the Final c'tor inside FinalInner c'tor inside the Final c'tor for fred inside the Final c'tor for otherFinal [ March 30, 2004: Message edited by: Richard Quist ] [ March 30, 2004: Message edited by: Richard Quist ]
Joined: Dec 20, 2001
Originally posted by Anupam Sinha: With my limited brain the only answer that comes to my mind is Inner classes. Is that the answer? That is, indeed, the answer. The following code fails:
This code fails because, when a class in initialized, the constructor of that object invokes the constructor of the parent class. In this case, the constructor of the parent class is private so we end up with a compiler error. There is no way to instantiate the class Super outside of the class definition, itself. However, the following code works just fine.
This works because an inner class is allowed to access private members of its enclosing instance, including the private constructor. Of course, if you were to take that second example and mark Super as final... As Kathy mentioned, I don't know that I can think of a good reason to ever do this in a real application. However, when it comes to the SCJP exam, you're required to know a lot of things that might never come up in a real application. :roll: I hope that helps, Corey [ March 30, 2004: Message edited by: Corey McGlone ]