aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class question" Watch "inner class question" New topic
Author

inner class question

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
How can I access inner class ten and it's integer variable x = 11 ?
CertPal Test
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
package testscripts;
public class Test
{
final Integer y=10;
class two
{
final int x;
public two()
{
x = 10;
class ten
{
int x = 11;
}
ten t1=new ten();
System.out.println("ten x value is"+t1.x);
}
}
public int get()
{
two two2 = new two();
return two2.x;
}
public static void main(String [] args)
{
Test one = new Test();
two t=one.new two();
System.out.println("X value is"+t.x);


System.out.println(one.y + one.get());

}
}
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
If ten is a class outside two's constructor, the code to access ten will be:


But since ten is a class defined inside two's constructor, I don't think there is any way to access it.
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
Helen you cant access the class two from outside.because it is local for the constructor.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Yeah.... I just modified my post. I just realized that ten is a class defined inside two's constructor.
naveena kamsali
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Janki Shah wrote:How can I access inner class ten and it's integer variable x = 11 ?
CertPal Test





hi friend,


i have tried the pgm what you have posted here. i have cleared your problem. check the below program:




public class one
{
final Integer y=10;
two t=new two();
public class two//class inside class one
{

final int x;
public two() //constructor of class two
{
x = 10;

class ten //sub-class in class two
{
ten(){
int x = 11;
System.out.println("inside class ten"+x);
}
}
ten t=new ten();

System.out.println("inside class two"+x);
}
}
public int get() // method in class one
{
System.out.println("below two stmts output is class one inside get() method");
two two2 = new two();
return two2.x;

}
public static void main(String [] args)
{
one one = new one();
System.out.println("sum of two no.s is:"+(one.y + one.get()));

}
}
output is:

inside class ten11
inside class two10
below two stmts is of class one inside get() method
inside class ten11
inside class two10
sum of two no.s is:20
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Eshwin Sukhdeve wrote:Helen you cant access the class two from outside.because it is local for the constructor.


So, if the ten class is ouside the two class's constructor but in side the two class than we can do this,

But if the class is inside the two class's constructor we can not use it. So, is this true always that you can not use any class's constructor code on an instance of that class even if there is an inner class defined in ?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Janki.
I have the same question as yours.
Since ten is an inner class defined inside Two's constructor, I don't know how to create or access an instance of ten.

Can anyone help?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859



Aside, ALL Class names should start with uppercase!

Pat.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

I think class inside a construtor is just like method level local class which is for the purpose of use and throw, don't need any more. but if you really need the ten's variable x any where outside the ten class, declare a new variable and store that into it. here is the code.



Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
These are the class files that I found after compiling the One class

1) One.class
2) One$two.class
3) One$two$1ten.class

In short, ten is an anonymous inner class, you cant access it past its place of use.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Yep - that's stupid! Please ignore the previous post.
 
 
subject: inner class question