File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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:
class NormalClass {
int i = 100;
static class NestedClass {
public int geti() { return new NormalClass().i; }
public class Test {
public static void main(String[] args) {
NormalClass.NestedClass x = new NormalClass.NestedClass();
NormalClass.NestedClass y = new NormalClass().new NestedClass();
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:
Does anybody understand what is going on???
[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

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.
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
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.

------------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

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 -
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
I agree. Here's the link:
subject: Static Inner Class wierdness
It's not a secret anymore!