Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
peter kens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from mughal's page 229
---------

public class AccessInTopLevelClass {
public void nonStaticMethod() {
System.out.println("nonstaticMethod in AccessInTopLevelClass ");
}
private static class NestedTopLevelClass {
private static int i;
private int j;
public static void staticMethod() {
System.out.println("staticMethod in NestedTopLevelClass");

}


interface NestedTopLevelInterface1 {
int Y2K = 2000;
}
protected static class NestedTopLevelClass1 implements NestedTopLevelInterface1 {
private int k = Y2K;
public void anotherNonStaticMethod() {
//int jj = j;
int ii = i;
int kk = k;
//nonStaticMethod();
staticMethod();
}
public static void main (String args[]) {
int ii = i;
//int kk = k;
staticMethod();
}
}
}
}
-----------------------------------------
when i compile the above java file
this is the output
AccessInTopLevelClass$NestedTopLevelClass$NestedTopLevelClass1.class
AccessInTopLevelClass$NestedTopLevelClass$NestedTopLevelInterface1.class
AccessInTopLevelClass$NestedTopLevelClass.class
AccessInTopLevelClass.class
-------------------------------------------
I want to run this
when i say " java NestedTopLevelClass1 "
it says
"Exception in thread "main" java.lang.NoClassDefFoundError: NestedTopLevelClass1"
So How do i Run it ?
Thank you in Advance
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
The full name of a nested top-level calss includes the name of the class it is defined in ( See example on pg 226 mughal's ) . What this means is if you have a nested top level class X defined in a class Y , the full name of the class X would be Y$X . See the naming convention explained for example 7.1 on page 226 ( mughals ) .
In your code NestedTopLevelClass1 is defined in NestedTopLevelClass which in turn is defined in AccessInTopLevelClass.
So to execute NestedTopLevelClass1 you'd need the following line -
java AccessInTopLevelClass$NestedTopLevelClass$NestedTopLevelClass1.
Have a look at the names of the class files created to get the idea.
HTH
Ashish H.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic