| Author |
How to access inner class members of class Ten which is implemented inside construcor of class Two?
|
Sanjay Kumar Sah
Greenhorn
Joined: Nov 12, 2009
Posts: 3
|
|
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
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3087
|
|
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
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3087
|
|
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.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
|
Surely that local class will go out of scope when the constructor completes?
|
 |
Sanjay Kumar Sah
Greenhorn
Joined: Nov 12, 2009
Posts: 3
|
|
|
Thanks...,It really works.
|
 |
 |
|
|
subject: How to access inner class members of class Ten which is implemented inside construcor of class Two?
|
|
|