| Author |
Empty Private Constructor Class: Why?
|
Tom Marler
Greenhorn
Joined: Jul 05, 2011
Posts: 1
|
|
I am new to this forum, thanks for providing the platform! I have been starting to study Java and ran into a line of code from an example out of a book I could not make sense of, maybe somebody has an idea.
Why did they specify a private Constructor which is empty? They did it both for the outer class and also for the inner.
Thanks!
Tom
|
 |
Dan Din
Greenhorn
Joined: Jun 06, 2011
Posts: 12
|
|
Welcome, Tom!
I think the example want to demonstrate the relation between the Outer and Inner classes - in my code, i can create an instance to the outer class from the inner class (line 8), and vice-verse (line 18), even the constructor are private and cannot be accessed from outside.
I look forward for other explanation.
D.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Tom Marler wrote:
Why did they specify a private Constructor which is empty? They did it both for the outer class and also for the inner.
the whole purpose of this idiom is you cant create an object as well as cant subclass for both Outer and Inner classes from other classes.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
And Welcome to JavaRanch
|
 |
Unnar Björnsson
Ranch Hand
Joined: Apr 30, 2005
Posts: 164
|
|
|
This class is probably designed to be a singleton or static method provider, in either case it should not be instantiated by outside call, therefore the default constructor is overridden as private.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Unnar Björnsson wrote:therefore the default constructor is overridden as private.
I think, you meant *marked* as private. constructor can not be overridden
|
 |
 |
|
|
subject: Empty Private Constructor Class: Why?
|
|
|