• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final Classes

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

If a class has all private constructors,should it be declared as final?
Thanks in advance
Vineela
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class with private constructors can be instantiated but a final class can't be.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am extremely sorry for the wrong post. Actually I messed up with intializing and extending.
[ March 30, 2004: Message edited by: Anupam Sinha ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With my limited brain the only answer that comes to my mind is Inner classes. Is that the answer?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic