What is the difference between a static and instance variable. The following sentence is what I cant get:
In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used.
A static variable represents class wide info.All objects of a class share the same data.
I thought that instance vars were used class wide whereas static variables only had scope within their own methods?
In the context of class attributes,
static
has a different meaning. If you have a field like:then, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.
Thus said, you might understand that this is bad in many cases, because it can easiy turn into an undesired side-effect: changing object
a
also affectsb
and you might end up wondering whyb
changed with no apparent reasons. Anyway, there are cases where this behaviour is absolutely desirable:const
, having all the classes access the same value will do no harm, because no one can change that. They can save memory too, if you have a lot of instances of that class. Not sure about concurrent access, though.static
vars are instantiated before your program starts, so if you have too many of them, you could slow down startup.A
static
method can only accessstatic
attributes, but think twice before trying this.Rule of thumb: don't use
static
, unless it is necessary and you know what you are doing or you are declaring a class constant.Static(Class) variables and instance variables both are member variables because they are both associated with a specific class, but the difference between them is Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
Suppose we create a static variable K and in the main function we create three objects: ob1 ob2 ob3; All these objects can have the same value for variable K. In contrast if the variable K was an instance variable then it could have different values as: ob1.k ob2.k ob3.k
Instance Variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Static Variable would only be one copy of each class variable per class, regardless of how many objects are created from it.
I think you are thinking about the C/C++ definition of the static keyword. There, the static keyword has many uses. In Java, the static keyword's functionality is described in your post. Anyhow, you can try it for yourself:
and similarly for non static variables:
Consider a class
MyClass
, having one static and one non-static member:Now, let's create a
main()
to create a couple of instances:Result:
Now you can see value of the static variable printed
60
both the times, as bothobj1
andobj2
were referring to the same variable. With the non-static variable, the outputs differ, as each object when created keeps its own copy of non-static variable; changes made to them do not impact on the other copy of the variable created by another object.