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 Final Classes 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 "Final Classes" Watch "Final Classes" New topic
Author

Final Classes

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi All,

If a class has all private constructors,should it be declared as final?
Thanks in advance
Vineela
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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


"Eppur si muove!"
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
A class with private constructors can be instantiated but a final class can't be.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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, §8.1.1.2 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


SCJP Tipline, etc.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I am extremely sorry for the wrong post. Actually I messed up with intializing and extending.
[ March 30, 2004: Message edited by: Anupam Sinha ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
With my limited brain the only answer that comes to my mind is Inner classes. Is that the answer?
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
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 ]

Rich
SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Classes