Why isn't my program accessing the superclass' array as expected?

I searched for an answer to this problem for quite some time, and I have done a lot of different tests, and I have narrowed and simplified a bug in my Java Applet down to this small piece of code. I am guessing that this exact same bug would come up if it was not an Applet though.

Here is the Main class.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
public class Main extends Applet{

  public int[] a = {2};
  public int[] b = new int[1];

  public void init(){
    b[0] = 4;
    Otherclass s = new Otherclass();
  }
}

Here is the Otherclass.

import java.util.*;
import java.lang.*;
public class Otherclass extends Main{

  public Otherclass(){
    System.out.println(super.a[0]);
    System.out.println(super.b[0]);
  }
}

Expected Output:

2
4

Actual Output:

2
0

What am I doing wrong?

Thanks in advance, Micah

Jon Skeet
people
quotationmark

You're using b in the constructor of Otherclass - which will be executed before the Applet launcher gets to execute init.

If you put this statement:

b[0] = 4;

into the constructor of Main, it will be executed before the constructor body of Otherclass.

Note that your initialization of s in init is pointless.

people

See more on this question at Stackoverflow