This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages) (Learn how and when to remove this message)
|
Inclass-based,object-oriented programming, aninstance variable is avariable defined in aclass (i.e., amember variable), for which each instantiatedobject of the class has a separate copy, orinstance.[1][2] An instance variable has similarities with aclass variable,[3] but is non-static. In C++ or Java language, an instance variable is a variable which is declared in a class but outside ofconstructors,methods, orblocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class.Access modifiers can be given to the instance variable.
An instance variable is not aclass variable,[4] although there are similarities. Both are a type ofclass attribute (or class property,field, or data member). While an instance variable's value may differ between instances of a class, aclass variable can only have one value at any one time, shared between all instances. The same dichotomy betweeninstance andclass members applies to methods ("member functions") as well.
Each instance variable lives inmemory for thelifetime of the object it is owned by.[5]
Instance variables are properties of that object. All instances of a class have their own copies of instance variables, even if the value is the same from one object to another. One class instance can change values of its instance variables without affecting all other instances. A class may have both instance variables andclass variables.
Instance variables can be used by all instance methods of an object, but may not be used by class methods. An instance variable may also be changed directly, providedaccess restrictions are set.[6]
classRequest{private:staticinlineintcount1=0;intnumber;public:// constructor modifies the instance variable "this->number"Request():number{count1}{++count1;// modifies the class variable "Request::count1"}};
In thisC++ example, the instance variableRequest::number is a copy of theclass variableRequest::count1 where each instance constructed is assigned a sequential value ofcount1 before it isincremented. Sincenumber is an instance variable, eachRequest object contains its own distinct value; in contrast, there is only one objectRequest::count1 available to all class instances with the same value.
classMyInteger{privateintx=0;publicMyInteger(){}publicMyInteger(intx){this.x=x;}publicvoidset(intx){this.x=x;}publicintget(){returnthis.x;}}publicclassMain{publicstaticvoidmain(String[]args){MyIntegermyInt1=newMyInteger();MyIntegermyInt2=newMyInteger();// As set() is an instance method, it can also access the variablemyInt2.set(-10);assertmyInt1.get()==10;assertmyInt2.get()==-10;}}
In thisJava example, we can see how instance variables can be modified in one instance without affecting another.
classDog:def__init__(self,breed:str)->None:self.breed=breed# instance variable# dog_1 is an object# which is also an instance of the Dog classdog_1:Dog=Dog("Border Collie")
In the abovePython code, the instance variable is created when an argument is parsed into the instance, with the specification of the breed positional argument.