Testaccount
Testaccount

Reputation: 2911

Accessing Static variables

public class Bicycle {
        
    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;
        
    public Bicycle(int startCadence, int startSpeed, int startGear){
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

        id = ++numberOfBicycles;
    }
       // ...
}

Source of code : https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

I learned in my class that Static variables should be accessed by calling with class name. i.e. ClassName.VariableName

But in the code above, how is this statement id = ++numberOfBicycles; compiled without errors, even though the variable numberOfBicycles is static

Upvotes: 15

Views: 54782

Answers (8)

bassam
bassam

Reputation: 11

you didn’t write Bicycle.numberOfBicycles. It isn’t necessary because we are already in that class so the compiler can infer it.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68935

Static variables are owned by class rather than by its individual instances (objects). Referring static variables outside the class is by ClassName.myStaticVariable but inside the class it is similar to other instance variables.

You can always use static variables in non-static methods but you cannot use non-static variables in static methods reason being when static methods are loaded other non-static instance variables are not created.

So your statement id = ++numberOfBicycles; is perfectly valid and will compile without errors.

Upvotes: 24

Manny265
Manny265

Reputation: 1709

 public int getID(){
    return numberOfBicycles;
}

public static int getNOB(){
    return numberOfBicycles;
}


In the Bicycle class

    Bicycle bc = new Bicycle(30, 90, 1);
    System.out.println(Bicycle.getNOB());
    System.out.println(bc.getID());

    Bicycle bc2 = new Bicycle(30,90, 1);
    System.out.println(Bicycle.getNOB());
    System.out.println(bc2.getID());

    Bicycle bc3 = new Bicycle(30,90, 1);
    System.out.println(Bicycle.getNOB());
    System.out.println(bc3.getID());

    Bicycle bc4 = new Bicycle(30,90, 1);
    System.out.println(Bicycle.getNOB());
    System.out.println(bc4.getID());


In the main class of BicycleTest worked just fine for me

Upvotes: 2

Manny265
Manny265

Reputation: 1709

Non static methods can access static members of a class because only a single copy of the static variable exists unlike instance variables which are only created once a new object of that type has been created.
I recommend you have another class to test,like BicycleTest which will have the main class and then create maybe 4Bicycle objects and using 2getters in the Bicycle class retrieve the numberofBicycles and ID everytime you create an object maybe that will give you a picture of what is happening.

Upvotes: 1

Khaled.K
Khaled.K

Reputation: 5940

Given your class ..

public class Bicycle
{
    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;

    // ..
}

When I create an objects of type Bicycle, it will be like this:

Bicycle a = new Bicycle (1,2,3);

Bicycle b = new Bicycle (2,3,4);

In memory, it's like this:

[a] --> { id:1, cadence:1, gear:2, speed:3 }

[b] --> { id:2, cadence:2, gear:3, speed:4 }

numberOfBicycles is static, so it's not part of any Bicycle object, it's related to the class not an object, and so it will be like this in memory:

[Bicycle] --> { numberOfBicycles:2 }

And so to access the static member, first we define a static getter for it:

public static int getNumberOfBicycles ()
{
    return numberOfBicycles;
}

then we call it from the class:

System.out.println(Bicycle.getNumberOfBicycles());

Upvotes: 1

pinkpanther
pinkpanther

Reputation: 4808

May be what your lecturer said is regarding accessing them from outside the class not from inside the class. static variables can be accessed outside the class like this ClassName.VariableName or object.VariableName. But however the first method is preferrable.

From inside the class it's not needed you may use this keyword or classname-qualifier to disambiguate with the local variables with the same name inside methods and constructors.

Upvotes: 5

Zim-Zam O'Pootertoot
Zim-Zam O'Pootertoot

Reputation: 18148

From within the class the Bicycle qualifier is optional on static variables, just like the this qualifier is optional on instance variables

Upvotes: 7

Juned Ahsan
Juned Ahsan

Reputation: 68715

Static variables are the shared variables. So you can access them using either the Classname.staticVariable or using an object of the class instance.staticVariable. In any case you will be referring to the single copy of the variable in memory, no matter how many objects you create.

Upvotes: 5

Related Questions