Why is variable declaration order important ?

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

Dario

Alan said:
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.

In the first example during the execution of
public static final Category UNCATEGORIZED
= new Category("Uncategorized");
the value of allCategories is still null.

In the second example during the execution of
public static final Category UNCATEGORIZED
= new Category("Uncategorized");
the value of allCategories is not null.

This is the expected behaviour for all Java Virtual Machines
because the initialization orders of static class variables
UNCATEGORIZED, SCIFI and allCategories
is defined by the language as the same textual order
in the source program.

Just my 2 cents.

- Dario
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top