File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes again! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes again!" Watch "inner classes again!" New topic
Author

inner classes again!

Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Hi All:
Have you tried nested inner class? (Inner class within an inner class). Well, it may not be really useful for certification purposes so if you see this post when looking from a certification point of view, you may choose to skip this.
This is my program:
public class stat7 {
class inner {
int i=2; // variable in the inner class 'inner'
class inner2 {
System.out.println(i); // A
} // end of nested inner class 'inner2'
} // end of class inner
public static void main(String[] args) {
stat7.inner i1 = new stat7().new inner();
System.out.println(i1.i); // B
stat7.inner.inner2 i2 = i1.new inner2();
System.out.println(i2.i); // C
}
} // end of class stat7
My question:
This program doesn't compile. Why? Compiler error at line marked C. Statement marked A works fine so I would expect C to work fine too. Am I missing something here? I would appreciate if someone could give some input. Thanks in advance.
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Viji,
I would be surprised if you don't get a compile time error at line marked A. Because, you are trying to declare a statement as a member of class inner2. I understand that the 4 possible things that can be declared in a class are (1) variables (2) methods (3) static initializers and (4) dynamic initializers.
The statement System.out.println(i); cannot fit into any of these categories. I am sure such thing won't work even in top level class, let alone in a nested class.
I modified your code slightly. I included your print statement in a method and called in main(). It works fine. Check it out.
Do you agree with me?

public class stat7 {
class inner {
int i=2; // variable in the inner class 'inner'
class inner2 {
void amethod() {
System.out.println(i); // A
}
} // end of nested inner class 'inner2'
} // end of class inner
public static void main(String[] args) {
stat7.inner i1 = new stat7().new inner();
System.out.println(i1.i); // B
stat7.inner.inner2 i2 = i1.new inner2();
//System.out.println(i2.i); // C
i2.amethod();
}
} // end of class stat7

Ying Xing
Greenhorn

Joined: Sep 23, 2000
Posts: 24
Hi,
Statment A does not complie on JDK1.3, you should put it inside a block.
Class inner2 does not declare a variable i, here i belongs to class inner, so you cannot access i using a reference of inner2.
public class stat7 {
class inner {
int i=2; // variable in the inner class 'inner'
class inner2 {
{
System.out.println(i); // A
}
} // end of nested inner class 'inner2'
} // end of class inner
public static void main(String[] args) {
stat7.inner i1 = new stat7().new inner();
System.out.println(i1.i); // B
stat7.inner.inner2 i2 = i1.new inner2();
//System.out.println(i2.i); // C
}
} // end of class stat7

regards,
[This message has been edited by Ying Xing (edited September 25, 2000).]
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Ramesh,
Statement A does not give a compile error. Well, I had the print statement there just to show that I can have the value of i printed there. I was not trying to find out if it is correct to have that statement there. I think it will be better if I rephrase my question. Well, my doubt is this. Ying, Agreed that i is not declared in class inner2. But, class inner2 is inner to class inner which means should class inner2 not be able to access the member variable of class inner? which in this case is variable i? Am I missing something here? I am confused that statement C doesn't even compile. If someone can give an explanation as to what is happening here, I would appreciate it.
Ying Xing
Greenhorn

Joined: Sep 23, 2000
Posts: 24
Hi Viji,
I think you've mixed up inner classes and inheritance. If inner2 extends inner, objectInner2.i will be ok!

regards,
ying
 
wood burning stoves
 
subject: inner classes again!