Ok, being much confused myself, I did a bit of research on this.
It turns out to be a bug in how Processing compiles, not in Java or anything else.
Try compiling this code with Sun's Java compiler (I used jdk 1.5.0_05):
Code:
public class sketch_051125a {
int test[]={1,2,3,4};
class TestClass
{
TestClass()
{
if (test!=null) System.out.println("TestClass()");
evaluate();
}
public void evaluate()
{
if (test!=null) System.out.println("TestClass.evaluate()");
}
}
class TestClass2 extends TestClass
{
int i = 10;
TestClass2()
{
i = 45;
if (test!=null) System.out.println("TestClass2()");
}
public void evaluate()
{
if (test!=null)
System.out.println("TestClass2.evaluate()");
System.out.println("i="+i);
}
}
void setup()
{
// TestClass c=new TestClass(); // this works fine
TestClass2 c2=new TestClass2(); // this breaks
}
public static void main(String args[])
{
(new sketch_051125a()).setup();
}
}
Try this in Processing v0095-expert and you get a NullPointerException. The reason is because Jikes isn't setting the inner class instance TestClass2's pointer to its enclosing class until some time after TestClass's constructor finishes (and presumably before TestClass2's constructor starts).
On a side note, Java seems to perform perfectly fine with running overridden methods from super class constructors. You'll notice i=0 is output by the above code (before it has been initialized to 10 or 45).
So no, this is not "how java works" but a bug related specifically to Processing's Java compiler.
Marcello