Why is variable declaration order important ?

Discussion in 'Java' started by Alan Franklin, Feb 2, 2004.

  1. 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.
     
    Alan Franklin, Feb 2, 2004
    #1
    1. Advertising

  2. Alan Franklin

    Dario Guest

    Alan Franklin wrote:

    > 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
     
    Dario, Feb 2, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D r .  P r o z a c

    Identify letter combinations where order is not important?

    D r . P r o z a c, Oct 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    428
    Micah Cowan
    Oct 20, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    906
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,977
    Smokey Grindel
    Dec 2, 2006
  4. Bolin
    Replies:
    4
    Views:
    411
  5. zalek
    Replies:
    1
    Views:
    967
    Tom Cole
    Aug 8, 2008
Loading...

Share This Page