Class variable

Class variable

In object-oriented programming with classes, a class variable is a variable defined in a class (i.e. a member variable) of which a single copy exists, regardless of how many instances of the class exist.[1][2][3][4]

A class variable is the opposite of an instance variable. It is a special type of class attribute (or class property, field, or data member).

In Java, C#, and C++, class variables are declared with the keyword static, and may therefore be referred to as static member variables.

The same dichotomy between instance and class members applies to methods ("member functions") as well; a class may have both instance methods and class methods. Again, Java, C#, and C++ use the keyword static to indicate that a method is a class method ("static member function").

Example

struct Request {
 
static int count;
int number;
 
    Request() {
        number = count; // modifies the instance variable "this->number"
       ++count; // modifies the class variable "Request::count"
    }
 
};
 
int Request::count = 0;

In this C++ example, the class variable Request::count is incremented on each call to the constructor, so that Request::count always holds the number of Requests that have been constructed, and each new Request object is given a number in sequential order. Since count is a class variable, there is only one object Request::count; in contrast, each Request object contains its own distinct number field.

Notes


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Class (format de fichier) — Classe Java Extension de fichier .class Type MIME application/java vm Développé par Sun Microsystems Type de format …   Wikipédia en Français

  • Variable Pocket Gopher — Conservation status Least Concern (IUCN 3.1)[1] …   Wikipedia

  • Variable-order Markov model — Variable order Markov (VOM) models are an important class of models that extend the well known Markov chain models. In contrast to the Markov chain models, where each random variable in a sequence with a Markov property depends on a fixed number… …   Wikipedia

  • Variable Damselfly — Scientific classification Kingdom: Animalia Phylum …   Wikipedia

  • Variable Checkerspot — E. chalcedona in William O. Douglas Wilderness Scientific classification Kingdom …   Wikipedia

  • Variable Sunbird — Conservation status Least Concern (IUC …   Wikipedia

  • Variable Wheatear — Conservation status Least Concern (IUCN 3.1) Scientific classification King …   Wikipedia

  • Variable Dwarf Kingfisher — Conservation status Least Concern ( …   Wikipedia

  • Variable star — A star is classified as variable if its apparent brightness as seen from Earth changes over time, whether the changes are due to variations in the star s actual luminosity, or to variations in the amount of the star s light that is blocked from… …   Wikipedia

  • class (format de fichier) — Classe Java Extension .class Type MIME application/java vm Développé par Sun Microsystems Type de format …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”