Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How the inner class are instantiated ?

 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





class B is the inner class of Class A

I want to instantiate the class B in the Class test

which means

i have to use dot operator as

outerClass.innerClass refVar = new outerClass.innerClass();

Its fine for one inner class

But what about if there is one more innerclass in B as


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create a non-static inner class Object you need an instance of the enclosing class. Like this:


With more classes then it just builds up:

But I don't think that you'll ever need such a construction.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if the inner class is static i.e

if class B is static ?

even though it doesnt matter the

A.B ab = new A().new B()

Am i correct ?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If B is static then you can't instantiate it with:

But with:


But haven't you tried it? You'll learn much more.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had tried .

But i was not sure about instantiation related to static .

Thank you for clearing my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic