• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Empty Private Constructor Class: Why?

 
Tom Marler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to JavaRanch
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unnar Björnsson wrote:therefore the default constructor is overridden as private.


I think, you meant *marked* as private. constructor can not be overridden
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic