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!
相关问题
- Delete Messages from a Topic in Apache Kafka
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
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:
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 callingMuffin.calories
. In this case, thefinal
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.So now you can access this specific muffin's flavor by calling
myMuffin.flavor
. Notice how we need to instantiate aMuffin
object before we can access itsflavor
.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:
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. Thestatic
keyword makes the value ofnext_id
persist through every call to the constructor, so theid
will be different and continue to increase for every new muffin.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
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.
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:
usage:
Let's say you have a blueprint of a car called
ToyotaYaris
, in which you might have a variable calledmaxSpeed
. 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 themaxSpeed
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 ofToyotaYaris
.Therefore:
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.