• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Inner Class wierdness

 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my effort to understand inner classes, I wrote the following (simple) piece of code:
<PRE>
class NormalClass {
int i = 100;
static class NestedClass {
public int geti() { return new NormalClass().i; }
}
}
</PRE>
public class Test {
public static void main(String[] args) {
NormalClass.NestedClass x = new NormalClass.NestedClass();
NormalClass.NestedClass y = new NormalClass().new NestedClass();
System.out.println(x.geti());
System.out.println(y.geti());
}
}
The code compiles without any errors, however, I get the following message when I run: java Test
Exception in thread "main" java.lang.VerifyError: (class: Test, method: main signature: ([Ljava/lang/String ;)V) Expecting to find unitialized object on stack
Interestingly enough, when I run it this way: java -noverify Test, it produces the following output:
100
100
Does anybody understand what is going on???
Regards,
SP
[This message has been edited by Stephen Pride (edited September 25, 2000).]
[This message has been edited by Stephen Pride (edited September 25, 2000).]
[This message has been edited by Stephen Pride (edited September 25, 2000).]
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Stephene.
With jdk1.3 on Windows NT you don't have to specify -noverify to get this running. It does produce 100 100 output in either case.
-Ramesh
 
Viji Bharat
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen:
Well, there was a posting prior to this with a similar doubt about the inner class initialization and run-time error. I have given below Ajith's reply to this with the bug list included.
Look for posting titled - 'Static Inner class - help' for a complete discussion.

HTH.
------------Ajith's reply quoted here -------------
Interesting indeed.
After doing some digup I found that this bug has been logged in Suns's Bug parade.
See these related bugs-

Bug 4150190
Bug 4225105
Bug 4137335
The evaluation says
quote:

The compiler generates code for this program which does not even verify. Definitely a bug. It appears to me that the compiler, in the compound construction, does not recognize that the class is static -- it leaves an extra copy of the outer class on the stack!
This is a duplicate of 4225105. In that case, the extra argument is detected during the compiler's internal stack-depth balance checks, and causes a crash at compile time, but the underlying cause appears the same.
and the suggested work around says -
quote:
Do not use the short-handed initialization method. Break code into 2 separate stages. or remove make inner class/method non-static.
Hope this helps
Ajith
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic