HOME

TheInfoList



OR:

In
class-based Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance occurs via defining '' classes'' of objects, instead of inheritance occurring via the objects alone (compare proto ...
,
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, an instance variable is a variable defined in a class (i.e. a
member variable In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (''member functions''). In class-based programming languages, these a ...
), for which each instantiated
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ai ...
of the class has a separate copy, or instance. An instance variable has similarities with a class variable, but is non-
static Static may refer to: Places *Static Nunatak, a nunatak in Antarctica United States * Static, Kentucky and Tennessee * Static Peak, a mountain in Wyoming ** Static Peak Divide, a mountain pass near the peak Science and technology Physics *Static ...
. An instance variable is a variable which is declared in a class but outside of constructors,
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
s, 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 although there are similarities. It is a type of class attribute (or class property,
field Field may refer to: Expanses of open ground * Field (agriculture), an area of land used for agricultural purposes * Airfield, an aerodrome that lacks the infrastructure of an airport * Battlefield * Lawn, an area of mowed grass * Meadow, a grass ...
, or data member). The same dichotomy between ''instance'' and ''class'' members applies to methods ("member functions") as well; a class may have both
instance method A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any o ...
s and class methods. Each instance variable lives in memory for the
lifetime Lifetime may refer to: * Life expectancy, the length of time a person is expected to remain alive Arts, entertainment, and media Music * Lifetime (band), a rock band from New Jersey * ''Life Time'' (Rollins Band album), by Rollins Band * ...
of the object it is owned by. Variables are properties an object knows about itself. All instances of an object have their own copies of instance variables, even if the value is the same from one object to another. One object instance can change values of its instance variables without affecting all other instances. Instance variables can be used by all methods of a class unless the method is declared as static.


Example

struct Request ; 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 instances with the same value.


References

Object-oriented programming Variable (computer science) {{Compu-prog-stub