aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Inner Class wierdness 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 "Static Inner Class wierdness" Watch "Static Inner Class wierdness" New topic
Author

Static Inner Class wierdness

Stephen Pride
Ranch Hand

Joined: Sep 14, 2000
Posts: 121
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).]


SCJP
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
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

Joined: Sep 18, 2000
Posts: 101
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
 
 
subject: Static Inner Class wierdness