wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I get a runtime error when using static Inner Class -- pls explain. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I get a runtime error when using static Inner Class -- pls explain." Watch "I get a runtime error when using static Inner Class -- pls explain." New topic
Author

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

Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Sep 12, 2000
Posts: 36
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

Joined: Oct 04, 2000
Posts: 73
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I get a runtime error when using static Inner Class -- pls explain.
 
Similar Threads
why private member variable of inner class can't be accessed by its outer class but mehod can be?
accessing shadowed final values in method local inner class
about inner classes
Bug in K& B?
inner class instantiation from outside the the outer class