A
Alan Franklin
Whilst getting my head around how to construct typesafe enumerations. I
tripped over
the following bizarre behaviour in which the order in which the static
variables are
declared is critical.
Being relatively new to Java, I am not sure if this a bug in the Java spec,
or simply poor
implementations (eclipse and netbeans). Both examples below compile without
issue.
This first example crashes on startup with a null pointer exception.
import java.util.Vector;
public class Category {
private String name;
public static final Category UNCATEGORIZED = new Category("Uncategorized");
public static final Category SCIFI = new Category("Science Fiction");
private static Vector allCategories = new Vector();
private Category(String aCategory) {
super();
name = aCategory;
allCategories.add(this);
}
public static Vector categories()
{ return (Vector) allCategories.clone(); }
public static void main(String[] args) {
Category x;
x = Category.SCIFI;
}
}
This second works as expected.
import java.util.Vector;
public class Category {
private String name;
private static Vector allCategories = new Vector();
public static final Category UNCATEGORIZED = new Category("Uncategorized");
public static final Category SCIFI = new Category("Science Fiction");
private Category(String aCategory) {
super();
name = aCategory;
allCategories.add(this);
}
public static Vector categories()
{ return (Vector) allCategories.clone(); }
public static void main(String[] args) {
Category x;
x = Category.SCIFI;
}
}
What's different ? The position of the declaration
private static Vector allCategories = new Vector();
Whilst I can conceptually see what is happening, I would have expected the
compiliers to have
separated the allocation of memory space for an object from the execution of
object code. In
this case it *looks* like it is simply executed in sequence and the self
referencing brings it to grief.
Any Java Language gurus like to comment / explain ?
Alan.
tripped over
the following bizarre behaviour in which the order in which the static
variables are
declared is critical.
Being relatively new to Java, I am not sure if this a bug in the Java spec,
or simply poor
implementations (eclipse and netbeans). Both examples below compile without
issue.
This first example crashes on startup with a null pointer exception.
import java.util.Vector;
public class Category {
private String name;
public static final Category UNCATEGORIZED = new Category("Uncategorized");
public static final Category SCIFI = new Category("Science Fiction");
private static Vector allCategories = new Vector();
private Category(String aCategory) {
super();
name = aCategory;
allCategories.add(this);
}
public static Vector categories()
{ return (Vector) allCategories.clone(); }
public static void main(String[] args) {
Category x;
x = Category.SCIFI;
}
}
This second works as expected.
import java.util.Vector;
public class Category {
private String name;
private static Vector allCategories = new Vector();
public static final Category UNCATEGORIZED = new Category("Uncategorized");
public static final Category SCIFI = new Category("Science Fiction");
private Category(String aCategory) {
super();
name = aCategory;
allCategories.add(this);
}
public static Vector categories()
{ return (Vector) allCategories.clone(); }
public static void main(String[] args) {
Category x;
x = Category.SCIFI;
}
}
What's different ? The position of the declaration
private static Vector allCategories = new Vector();
Whilst I can conceptually see what is happening, I would have expected the
compiliers to have
separated the allocation of memory space for an object from the execution of
object code. In
this case it *looks* like it is simply executed in sequence and the self
referencing brings it to grief.
Any Java Language gurus like to comment / explain ?
Alan.