FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Class_variable

Class variable

In class-based, object-oriented programming, 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 not an instance variable. It is a special type of class attribute (or class property, field, or data member). 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.

classOrder{ private: inlinestaticintnextId=0; intid; public: Order(): id{nextId++}{} };

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

Also note that the variable Order::nextId is initialized only once (as static members that are instances of itself must provide the definitions outside the class. Trying to use declare an instance of a class as a static member of itself fails as no definition of an object may be an incomplete type, which the class is incomplete until it is closed.

ClassVar.

"The Java Tutorial, Understanding Instance and Class Members". Retrieved 2010-10-21.
  • ↑ "Class Variables in C#". Syntaxdb. Retrieved 1 April 2018.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home