Instance variable
Encyclopedia
In object-oriented programming
with class
es, an instance variable is a variable
defined in a class (i.e. a member variable
), for which each object
of the class has a separate copy. They live in memory for the life of the object.
An instance variable contrasts with a class variable
, and it is a special type of instance member. An example (C++ or Java) declaration of an instance variable is
Technically speaking, instance variables are objects stored in individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the
A simpler definition is that instance variables are things an object knows about itself.
A typical declaration in Objective-C would look like:
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
with class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
es, an instance variable is a variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
defined in a class (i.e. a member variable
Member variable
In object-oriented programming, a member variable is a variable that is associated with a specific class, and accessible for all its methods. If there is only one copy of the variable shared with all instances of the class, it is called a class variable or static member variable...
), for which each object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
of the class has a separate copy. They live in memory for the life of the object.
An instance variable contrasts with a class variable
Class variable
In object-oriented programming with classes, a 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 the opposite of an instance variable...
, and it is a special type of instance member. An example (C++ or Java) declaration of an instance variable is
private double length
.Technically speaking, instance variables are objects stored in individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the
currentSpeed
of one bicycle is independent from the currentSpeed
of another.A simpler definition is that instance variables are things an object knows about itself.
A typical declaration in Objective-C would look like:
@interface CustomClassName : NSObject{ NSString *ivar; } |