Instance variable
{{Short description|Member variable of a class that all its objects possess a copy of}}
{{distinguish|class variable}}
{{multiple issues|
{{original research|date=September 2013}}
{{tone|date=September 2013}}
}}
In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e., a member variable), for which each instantiated object of the class has a separate copy, or instance.{{Cite web |title=Instance Variables in C++ Programming |url=https://www.dremendo.com/cpp-programming-tutorial/cpp-instance-variables |access-date=2024-03-08 |website=Dremendo |language=en}}{{Cite web |date=2017-02-06 |title=Java Variables |url=https://www.geeksforgeeks.org/variables-in-java/ |access-date=2024-03-08 |website=GeeksforGeeks |language=en-US}} An instance variable has similarities with a class variable,{{cite web|title=The Java Tutorial, Variables|url=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|website=docs.oracle.com|publisher=Oracle|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141023153904/http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|archive-date=23 October 2014}} but is non-static. An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. 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 a class variable,{{Cite web |date=2021-04-26 |title=Difference between Instance Variable and Class Variable |url=https://www.geeksforgeeks.org/difference-between-instance-variable-and-class-variable/ |access-date=2024-03-08 |website=GeeksforGeeks |language=en-US}} although there are similarities. Both are a type of class attribute (or class property, field, or data member). While an instance variable's value may differ between instances of a class, a class variable can only have one value at any one time, shared between all instances. The same dichotomy between instance and class members applies to methods ("member functions") as well.
Each instance variable lives in memory for the lifetime of the object it is owned by.{{cite web|title=The Java Tutorials, Understanding Class Members|url=http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|website=docs.oracle.com|publisher=Oracle|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141011000515/http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|archive-date=11 October 2014}}
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 and class 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, provided access restrictions are set.{{cite web|last1=Matuszek|first1=David|title=Static|url=http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|website=cis.upenn.edu|publisher=University of Pennsylvania|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141023160745/http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|archive-date=23 October 2014}}
Examples
= C++ =
struct Request {
static int count1; // variable name is not important
int number;
Request() {
number = count1; // modifies the instance variable "this->number"
++count1; // modifies the class variable "Request::count1"
}
};
int Request::count1 = 0;
In this C++ example, the instance variable Request::number
is a copy of the class variable Request::count1
where each instance constructed is assigned a sequential value of count1
before it is incremented. Since number
is an instance variable, each Request
object contains its own distinct value; in contrast, there is only one object Request::count1
available to all class instances with the same value.
= Java =
//Example.java
class Example {
public int x = 0;
public void setX(int newValue) {
this.x = newValue;
}
}
//Main.java
class Main {
public static void main(String[] args) {
Example example1 = new Example();
Example example2 = new Example();
//We can set the value of x by itself, as the variable is public
example1.x = 10;
assert example1.x == 10;
assert example2.x == 0;
//As setX is an instance method, it can also access the variable
example2.setX(-10);
assert example1.x == 10;
assert example2.x == -10;
}
}
In this Java example, we can see how instance variables can be modified in one instance without affecting another.
= Python =
class Dog:
def __init__(self, breed):
self.breed = breed # instance variable
- dog_1 is an object
- which is also an instance of the Dog class
dog_1 = Dog("Border Collie")
In the above Python code, the instance variable is created when an argument is parsed into the instance, with the specification of the breed positional argument.
References
{{Reflist}}
{{DEFAULTSORT:Instance Variable}}
Category:Object-oriented programming
Category:Variable (computer science)
{{Compu-prog-stub}}