• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access inner class members of class Ten which is implemented inside construcor of class Two?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class One {
final Integer y = 10;

class Two //Class Two starts here
{
final int x;
public Two() //Constructor starts here
{
x = 10;

class Ten // Class Ten starts here
{
int x = 11;
} // Class Ten ends here

}// Constructor ends here
} //Class Two ends here
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay, I moved your topic to the Java In General forum because I think it is more appropriate here, rather than in IDEs...

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do so inside the Two constructor by creating a new instance of Ten, then accessing the value you want. Example:


Note that you can only do so inside the Two constructor. Since the class was defined inside the constructor then it only has scope inside that method. So as shown in the above code sample you can't use or refer to Ten even inside another method of the same class.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely that local class will go out of scope when the constructor completes?
 
Sanjay Kumar Sah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...,It really works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!