abstract class scope issue?

E

Elrod

The code below is a very simplified representation of a problem I've come
across several times in a program I've been working on:

public class TestClass extends AbstractClass {
String mystery = null;
public TestClass() {
System.out.println(mystery);
}

public void abstractMethod() {
System.out.println("abstractMethod called");
this.mystery = "some value";
}

public static void main(String[] args) {
TestClass tc = new TestClass();
}
}

abstract class AbstractClass {
public AbstractClass() {
abstractMethod();
}

public abstract void abstractMethod();
}

Now everything I know about this says that the output should be:

:abstractMethod called
:some value

however, when executed, the output is

:abstractMethod called
:null

I have found that moving the "mystery" variable declaration into
"AbstractClass" or moving the call to "abstractMethod" into the "TestClass"
constructor results in the expected output, but niether of these changes can
be applied to my real-world application in any useful manner. Also, in the
case that it is a known bug, I am running java 1.4.2_04.
Any explanations or suggestions regarding this issue would be much
appreciated.

Eldon Down
 
D

David Hilsee

Elrod said:
The code below is a very simplified representation of a problem I've come
across several times in a program I've been working on:

public class TestClass extends AbstractClass {
String mystery = null;
public TestClass() {
System.out.println(mystery);
}

public void abstractMethod() {
System.out.println("abstractMethod called");
this.mystery = "some value";
}

public static void main(String[] args) {
TestClass tc = new TestClass();
}
}

abstract class AbstractClass {
public AbstractClass() {
abstractMethod();
}

public abstract void abstractMethod();
}

Now everything I know about this says that the output should be:

:abstractMethod called
:some value

however, when executed, the output is

:abstractMethod called
:null

I have found that moving the "mystery" variable declaration into
"AbstractClass" or moving the call to "abstractMethod" into the "TestClass"
constructor results in the expected output, but niether of these changes can
be applied to my real-world application in any useful manner. Also, in the
case that it is a known bug, I am running java 1.4.2_04.
Any explanations or suggestions regarding this issue would be much
appreciated.

You are invoking a method on an uninitialized object. The assignment to
null occurs when the derived class's constructor executes, which is after
you have invoked abstractMethod(). In one of my tests, removing the
assignment to null did the trick, but I have no idea if that's merely
coincidence or required behavior. I would look for another way to do
whatever it is that you are trying to accomplish, because it is generally
considered bad form to invoke methods on an object that has not been
initialized, mostly because it may be in an invalid state.
 
C

Chris Uppal

Elrod said:
Now everything I know about this says that the output should be:


however, when executed, the output is

The important point is that static initialisers like:
String mystery = null;

are defined to be executed /after/ the superclass constructor, but /before/ the
body of the derived class's constructor.

You can think of it as if the compiler re-writes the TestClass constructor
to:

public TestClass()
{
super();
mystery = null;
System.out.println(mystery);
}

This is all defined in the JLS2, but it's too hot to go looking for references
just now...

-- chris
 
C

Chris Uppal

Mike said:
nitpick: this is an instance initializer, not a static initializer.

Oops!

Or -- to pick recursively smaller nits -- an "instance variable initializer"
;-)

-- chris
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top