Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I get a runtime error when using static Inner Class -- pls explain.

 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i tried to execute a program with static inner classes and tried to access the enclosing instance's static variable.
it compiles fine. but gives
"Exception in Thread "main" java.lang.VerifyError (class: myclass method: main signature: (LJava/lang/String V) Expecting to find uninitialized object on stack.
here is the code which created the above error at runtime.
class myouter{
int i ;
static int j;
myouter(){
System.out.println("Inside myouter()");
i =2;
j=3;
System.out.println("i is : "+i);
System.out.println("j is : "+j);
}
static class myinner{
myinner(){
System.out.println("Inside myinner()");
System.out.println("j is : "+j);
}//end of myinner()
}//end of myinner...
}
public class myclass{
public static void main(String args[]){
myouter.myinner in = new myouter().new myinner();
}//end of main
}
can anyone explain what is wrong with my code please? what am i missing ??
Thanks,
Sunitha. S
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code statement:
myouter.myinner in = new myouter().new myinner();
is the way to construct a member inner class, not
a static nested top-level class instance. As I recall, you want
myouter.myinner in = new myouter.myinner() ;
Bill
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill..
i also learnt that the static inner class can be called as follows...
myouter.myinner in = new myouter.myinnner();
Thanks,
Sunitha. S


[This message has been edited by Sunitha Sounderrajan (edited October 08, 2000).]
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunitha Sounderrajan,
I tried your code with
" myouter.myinner in = new myouter().new myinner();"
but I do not encounter error as you said.
Both "myouter.myinner in = new myouter().new myinner();"
and "myouter.myinner in = new myouter.myinner();"
give the same result.
why?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic