Enclosing class instance availability during inner class initialization

S

Stanimir Stamenkov

I've found couple of threads [1] discussing the exact same problem, but
I've wondered if there are some normative references governing the
change in the behavior I'm seeing between classes compiled with -target
1.4 and prior versions?

If I compile the following example with -target 1.4 and run it, I don't
get error, but If I compile it with no -target specified (or version <
1.4, using J2SDK 1.4.2) I get NullPointerException because of the
"missing" EnclosingInstanceTest instance at the given time. When I
compile with JDK 1.5 I don't get error because the default target seems
to be >= 1.4.

-----EnclosingInstanceTest.java
public class EnclosingInstanceTest {

public static void main(String[] args) {
EnclosingInstanceTest obj = new EnclosingInstanceTest();
System.out.println("Start...");
obj.doTest();
}

void doTest() {
Whatever ever = new Whatever() {
/* init */ {
System.out.println("(2) Enclosing instance: "
+ EnclosingInstanceTest.this);
}
void doSomething() {
System.out.println("(*) Enclosing instance: "
+ EnclosingInstanceTest.this);
testInstance();
}
};
ever.doSomething();
}

void testInstance() {
System.out.println("O.k.");
}

}


abstract class Whatever {

Whatever() {
System.out.println("(1) Super class init");
// XXX: Uncomment the following line to test
doSomething();
}

abstract void doSomething();

}
-----EnclosingInstanceTest.java--

[1] NullPointerException accessing instance member of enclosing
instance
<http://groups.google.com/group/comp...read/thread/81eeb5e4e0d53a33/3aafa2d47d87780a>,
Getting a null "this"
<http://groups.google.com/group/comp...read/thread/dd32223ae8a25867/c2c452392f0c7d70>
 
T

Thomas Hawtin

Stanimir said:
I've found couple of threads [1] discussing the exact same problem, but
I've wondered if there are some normative references governing the
change in the behavior I'm seeing between classes compiled with -target
1.4 and prior versions?

The URL leading to the information should be...

http://java.sun.com/j2se/1.4/compatibility.html

....but Sun have fucked up, so it doesn't.

Relevant bugs include:

"* Initialization of up-level links, immediately after super(), occurs
too late."
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4030374

"VM throws an error "java.lang.VerifyError" (-target 1.4 versus inner cls)"
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4416605

Tom Hawtin
 
S

Stanimir Stamenkov

Chris said:
A completely different kind of authority is the source, and the 1.5 source
there's a file

<jrl>/j2se/src/share/classes/com/sun/tools/javac/jvm/Target.java

Where do you get that source file from (I haven't understood the <jrl>
part)? Searching in my JDK 1.5 installation "src.zip" I don't really
find it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top