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.[1][2] An instance variable has similarities with a class 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 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,[4] 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.[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 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.[6]

Examples

edit

C++

edit
class Request {
private:
    static inline int count1 = 0;
    int number;
public:
    // constructor modifies the instance variable "this->number"
    Request():
        number{count1} {
        ++count1; // modifies the class variable "Request::count1"
    }

};

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

edit
class MyInteger {
    private int x = 0;

    public MyInteger() {}

    public MyInteger(int x) {
        this.x = x;
    }
    
    public void set(int x) {
        this.x = x;
    }

    public int get() {
        return this.x;
    }
}

public class Main {
    public static void main(String[] args) {
        MyInteger myInt1 = new MyInteger();
        MyInteger myInt2 = new MyInteger();
        
        // As set() is an instance method, it can also access the variable
        myInt2.set(-10);
        
        assert myInt1.get() == 10;
        assert myInt2.get() == -10;
    }
}

In this Java example, we can see how instance variables can be modified in one instance without affecting another.

Python

edit
class Dog:
    def __init__(self, breed: str) -> None:
        self.breed = breed # instance variable

# dog_1 is an object
# which is also an instance of the Dog class
dog_1: Dog = 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

edit
  1. ^ "Instance Variables in C++ Programming". Dremendo. Retrieved 2024-03-08.
  2. ^ "Java Variables". GeeksforGeeks. 2017-02-06. Retrieved 2024-03-08.
  3. ^ "The Java Tutorial, Variables". docs.oracle.com. Oracle. Archived from the original on 23 October 2014. Retrieved 23 October 2014.
  4. ^ "Difference between Instance Variable and Class Variable". GeeksforGeeks. 2021-04-26. Retrieved 2024-03-08.
  5. ^ "The Java Tutorials, Understanding Class Members". docs.oracle.com. Oracle. Archived from the original on 11 October 2014. Retrieved 23 October 2014.
  6. ^ Matuszek, David. "Static". cis.upenn.edu. University of Pennsylvania. Archived from the original on 23 October 2014. Retrieved 23 October 2014.

📚 Artikel Terkait di Wikipedia

Class variable

class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not

Member variable

class variables (also called static member variables), where only one copy of the variable is shared with all instances of the class; and instance variables

Ruby (programming language)

Python by using a publicly exposed instance variable, and later changes the implementation to use a private instance variable exposed through a property descriptor

Objective-C

to hold all the instance variables for an object, sets all the instance variables to zero values, and turns the memory into an instance of the class; at

Smalltalk

by all. The two kinds of variables commonly used in Smalltalk are instance variables and temporary variables. Other variables and related terminology depend

Class (programming)

be associated with state variables at program run time; these state variables either belong to the class or specific instances of the class. In most languages

Variable (high-level programming language)

For instance, a variable might be referenced by the identifier "total_count" and the variable can contain the number 1956. If the same variable is referenced

Ruby syntax

does not require the use of parentheses, it is trivial to change an instance variable into a full function without modifying a single line of calling code