Object Variables vs Class Variables in Java

2020-02-10 04:22发布

问题:

I am in the process of learning Java and I don't understand the difference between Object Variables and Class Variable. All I know is that in order for it to be a Class Variable you must declare it first with the static statement. Thanks!

回答1:

In Java (and in OOP in general) the objects have two kinds of fields(variable).

Instance variables(or object variable) are fields that belong to a particular instance of an object.

Static variables (or class variable) are common to all the instances of the same class.

Here's an example:

public class Foobar{
    static int counter = 0 ; //static variable..all instances of Foobar will share the same counter and will change if such is done
    public int id; //instance variable. Each instance has its own id
    public Foobar(){
        this.id = counter++;
    }
}

usage:

Foobar obj1 = new Foobar();
Foobar obj2 = new Foobar();
System.out.println("obj1 id : " + obj1.id + " obj2.id "+ obj2.id + " id count " + Foobar.counter);


回答2:

The difference between a static variable or a class variable and an instance variable or a object variable is pretty simple. Every object you create has its own copy of its very own instance variables. Where as if there is a static variable in the class then only one copy of that static variable exists for all the objects. For example

public class JellyBean{

    // instance variables every jellyBean object will have its own 
    // variable for color
    String color;

    // static variable only one copy of this variable exists for 
    // all jellyBean objects.
    static int totalNumberOfJellyBeans;

}//end class 

If you created 2 jellybean objects you would have two variables for Color because each jellybean has its own variable for color. And 1 variable for totalNumberOfJellyBeans because both jellyBean objects use this one class variable.



回答3:

An object variable is state dependent on a specific instance of a class, whereas a class variable is globally accessible through the class itself. That might be a little fuzzy, so here are some examples:

class Muffin {

    private static final int calories = 9320;

    public String flavor;

    public Muffin( String flavor ){
        this.flavor = flavor;
    }

}

In this class, calories is a class variable. In any other piece of code, you can get the number of calories in any kind of muffin by calling Muffin.calories. In this case, the final keyword is also used to make the number of calories constant.

In the same class, we have an object variable, flavor. This is dependent on the instance of the class, and is set in the constructor.

Muffin myMuffin = new Muffin( "blueberry" );

So now you can access this specific muffin's flavor by calling myMuffin.flavor. Notice how we need to instantiate a Muffin object before we can access its flavor.

Changing static (class) variables

The above example is a bit of a stretch, since different types of muffins would have different calorie counts. They are useful for constants, but here's a case where the value of the static variable changes:

class Muffin {

    private static int next_id = 1;

    public int id;
    public String flavor;

    public Muffin( String flavor ){
        this.flavor = flavor;
        id = next_id++;
    }

}

In the second example, we need to have a unique ID number for every muffin we create, so we can have a static variable that gets incremented every time a Muffin is instantiated. The static keyword makes the value of next_id persist through every call to the constructor, so the id will be different and continue to increase for every new muffin.



回答4:

Let's say you have a blueprint of a car called ToyotaYaris, in which you might have a variable called maxSpeed. All the cars made with that blueprint (its instances) will have the same maximum speed, so that variable should belong to the blueprint, and not the individual cars. If the maxSpeed changes in the blueprint, so will it change in all the cars it produces.

However, on each car, you might want to have another variable called speed. This variable can't belong to the blueprint because each car can be driving at different speeds independently of each other, so you need that variable to belong to each specific instance of ToyotaYaris.

Therefore:

class ToyotaYaris {
    static int maxSpeed;
    int speed;
}


回答5:

An object variable or instance member belongs to a specific instance of a class. That is to say that every instance has its own copy of that piece of data. A class variable or static member is shared by every instance of the class. That is to say that there is only one copy of that piece of data no matter how many class instances there are.