Instance variable

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. 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]

  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.