Stop Downvoting Me You Cunts Stop Downvoting Me You Cunts - 3 months ago 13
Java Question

What is the order in which code is loaded into memory, in particular static variables?

I considered myself a Java noob, so the answer to this question may need a bit of dumbing down in terms of terminology. I'd like to know nonetheless.

I've created a class, Keyboard, with a variable, AutoNumber, of type double which value 50. In this class is one no-arg constructor, and a second constructor that accepts an int value.

public class Console {
public static void main(String[] args){
Keyboard Key1 = new Keyboard(25);
Keyboard Key2 = new Keyboard();
Keyboard Key3 = new Keyboard(355);
System.out.println("Key 1 is " + Key1.AutoNumber);
System.out.println("Key 2 is " + Key2.AutoNumber);
System.out.println("Key 3 is " + Key3.AutoNumber);
}
}
class Keyboard {
static double AutoNumber = 50;
Keyboard(){
}
Keyboard(int NewAutoNumber){
AutoNumber = NewAutoNumber;
}
}


The output is as follows:


Key 1 is 355.0

Key 2 is 355.0

Key 3 is 355.0


The interesting thing is that the value of Key 1-3 takes the form of whatever number in the last compiled arg-constructor. So if I change my code:

Keyboard Key3 = new Keyboard(355);
Keyboard Key1 = new Keyboard(25);
Keyboard Key2 = new Keyboard();


The output will be:


Key 1 is 25

Key 2 is 25

Key 3 is 25


I know how to make the output correspond with the correct value (key 1 = 25, key2 = 50, key3 = 355) and to do that I remove the static declaration of AutoNumber which fixes it. I'm just not sure why my program is acting the way it is. Can someone explain what I'm doing here? I know it's something to do with the way in which variables are loaded into memory from the onset of compilation, but just that so far.

Answer

The variable is static, so whether you set/get it from one object or the other is irrelevant: there is a single variable, shared between all objects.

And the lines in a method are executed from top to bottom. It's as simple as that.

In the first example:

  • first line sets the variable to 25
  • second line doesn't set the variable
  • third line overwrites the variable to 355
  • subsequent lines all print the same variable, whose value is 355

The explanation for the second example is identical.

You should never access a static variable or method using a reference to an object. That's bad practice and confusing.

Use Keyboard.AutoNumber, not Key1.AutoNumber.

Also, variables conventionally start with a lowercase letter.

So, your code should be written this way, which should make things clearer:

public class Console {
    public static void main(String[] args){
        Keyboard key1 = new Keyboard(25);
        Keyboard key2 = new Keyboard();     
        Keyboard key3 = new Keyboard(355);
        System.out.println("AutoNumber is " + Keyboard.autoNumber);
        System.out.println("AutoNumber is " + Keyboard.autoNumber);
        System.out.println("AutoNumber is " + Keyboard.autoNumber);
    }
}   
class Keyboard {
    static double autoNumber = 50;
    Keyboard(){
    }
    Keyboard(int newAutoNumber){
        Keyboard.autoNumber = newAutoNumber;
    }
}
Comments