Enclosing class instance availability during inner class initialization

Discussion in 'Java' started by Stanimir Stamenkov, Jul 13, 2006.

  1. 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.lang.java.programmer/browse_thread/thread/81eeb5e4e0d53a33/3aafa2d47d87780a>,
    Getting a null "this"
    <http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/dd32223ae8a25867/c2c452392f0c7d70>

    --
    Stanimir
     
    Stanimir Stamenkov, Jul 13, 2006
    #1
    1. Advertising

  2. Stanimir Stamenkov wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jul 13, 2006
    #2
    1. Advertising

  3. Chris Uppal wrote:

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

    --
    Thanks,
    Stanimir
     
    Stanimir Stamenkov, Jul 14, 2006
    #3
  4. Stanimir Stamenkov wrote:
    > Chris Uppal wrote:
    >
    >> 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.


    Sorry for asking the obvious (I've missed it): "the 1.5 source":

    "J2SE 5.0 JDK Source Code"
    <http://java.sun.com/javase/downloads/index.jsp>

    --
    Stanimir
     
    Stanimir Stamenkov, Jul 14, 2006
    #4
  5. Stanimir Stamenkov

    Chris Uppal Guest

    Stanimir Stamenkov wrote:

    > "J2SE 5.0 JDK Source Code"
    > <http://java.sun.com/javase/downloads/index.jsp>


    You beat me to it ;-)

    But, seriously, I hope you read the license conditions carefully before
    accepting them -- I would have warned you but you were too quick for me...

    -- chris
     
    Chris Uppal, Jul 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tor Iver Wilhelmsen
    Replies:
    0
    Views:
    427
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    449
    Jayaram
    Jul 18, 2003
  3. -
    Replies:
    1
    Views:
    13,659
  4. Jonathan
    Replies:
    3
    Views:
    436
  5. bugbear
    Replies:
    4
    Views:
    450
    bugbear
    Aug 23, 2006
Loading...

Share This Page