<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Instance_variable</id>
	<title>Instance variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Instance_variable"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Instance_variable&amp;action=history"/>
	<updated>2026-04-06T15:39:26Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Instance_variable&amp;diff=121853&amp;oldid=prev</id>
		<title>imported&gt;QuantumNinus at 01:44, 9 November 2025</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Instance_variable&amp;diff=121853&amp;oldid=prev"/>
		<updated>2025-11-09T01:44:53Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Member variable of a class that all its objects possess a their own copy of}}&lt;br /&gt;
{{distinguish|Class variable}}&lt;br /&gt;
{{multiple issues|&lt;br /&gt;
{{original research|date=September 2013}}&lt;br /&gt;
{{tone|date=September 2013}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
In [[class-based]], [[object-oriented programming]], an &amp;#039;&amp;#039;&amp;#039;instance variable&amp;#039;&amp;#039;&amp;#039; is a [[variable (programming)|variable]] defined in a [[Class (programming)|class]] (i.e., a [[member variable]]), for which each instantiated [[Object (computer science)|object]] of the class has a separate copy, or [[Instance (computer science)|instance]].&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt; An instance variable has similarities with a [[class variable]],&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt; but is non-[[Static variable|static]]. In C++ or Java language, an instance variable is a variable which is declared in a class but outside of [[Constructor (object-oriented programming)|constructor]]s, [[Method (computer programming)|method]]s, or [[Block (programming)|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.&lt;br /&gt;
&lt;br /&gt;
An instance variable is not a [[class variable]],&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt; although there are similarities. Both are a type of [[Class (computer science)#Structure|class attribute]] (or class property, [[Field (computer science)|field]], or data member). While an instance variable&amp;#039;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 &amp;#039;&amp;#039;instance&amp;#039;&amp;#039; and &amp;#039;&amp;#039;class&amp;#039;&amp;#039; members applies to methods (&amp;quot;member functions&amp;quot;) as well.&lt;br /&gt;
&lt;br /&gt;
Each instance variable lives in [[Computer memory|memory]] for the [[Object lifetime|lifetime]] of the object it is owned by.&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 variable|class variables]].&lt;br /&gt;
&lt;br /&gt;
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 modifiers|access restrictions]] are set.&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Request {&lt;br /&gt;
&lt;br /&gt;
    static int count1; // variable name is not important&lt;br /&gt;
    int number;&lt;br /&gt;
&lt;br /&gt;
    Request() {&lt;br /&gt;
        number = count1; // modifies the instance variable &amp;quot;this-&amp;gt;number&amp;quot;&lt;br /&gt;
        ++count1; // modifies the class variable &amp;quot;Request::count1&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int Request::count1 = 0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this [[C++]] example, the instance variable &amp;lt;code&amp;gt;Request::number&amp;lt;/code&amp;gt; is a copy of the [[class variable]] &amp;lt;code&amp;gt;Request::count1&amp;lt;/code&amp;gt; where each instance constructed is assigned a sequential value of &amp;lt;code&amp;gt;count1&amp;lt;/code&amp;gt; before it is [[increment operator|incremented]]. Since &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; is an instance variable, each &amp;lt;code&amp;gt;Request&amp;lt;/code&amp;gt; object contains its own distinct value; in contrast, there is only one object &amp;lt;code&amp;gt;Request::count1&amp;lt;/code&amp;gt; available to all class instances with the same value.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
//Example.java&lt;br /&gt;
&lt;br /&gt;
class Example {&lt;br /&gt;
    public int x = 0;&lt;br /&gt;
    &lt;br /&gt;
    public void setX(int newValue) {&lt;br /&gt;
        this.x = newValue;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//Main.java&lt;br /&gt;
&lt;br /&gt;
class Main {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Example example1 = new Example();&lt;br /&gt;
        Example example2 = new Example();&lt;br /&gt;
        &lt;br /&gt;
        //We can set the value of x by itself, as the variable is public&lt;br /&gt;
        example1.x = 10;&lt;br /&gt;
        &lt;br /&gt;
        assert example1.x == 10;&lt;br /&gt;
        assert example2.x ==  0;&lt;br /&gt;
        &lt;br /&gt;
        //As setX is an instance method, it can also access the variable&lt;br /&gt;
        example2.setX(-10);&lt;br /&gt;
        &lt;br /&gt;
        assert example1.x ==  10;&lt;br /&gt;
        assert example2.x == -10;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this [[Java (programming language)|Java]] example, we can see how instance variables can be modified in one instance without affecting another.&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    def __init__(self, breed):&lt;br /&gt;
        self.breed = breed # instance variable&lt;br /&gt;
&lt;br /&gt;
# dog_1 is an object&lt;br /&gt;
# which is also an instance of the Dog class&lt;br /&gt;
dog_1 = Dog(&amp;quot;Border Collie&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;In the above [[Python (programming language)|Python]] code, the instance variable is created when an argument is parsed into the instance, with the specification of the breed positional argument.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Instance Variable}}&lt;br /&gt;
[[Category:Object-oriented programming]]&lt;br /&gt;
[[Category:Variable (computer science)]]&lt;/div&gt;</summary>
		<author><name>imported&gt;QuantumNinus</name></author>
	</entry>
</feed>