<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Class_variable</id>
	<title>Class variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Class_variable"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Class_variable&amp;action=history"/>
	<updated>2026-12-01T06:08:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=Class_variable&amp;diff=121950&amp;oldid=prev</id>
		<title>~2026-41267-59 at 00:40, 24 July 2026</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Class_variable&amp;diff=121950&amp;oldid=prev"/>
		<updated>2026-07-24T00:40:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Variable defined in a class whose objects all possess the same copy}}&lt;br /&gt;
{{Portal|Computer programming}}&lt;br /&gt;
In [[class-based]], [[object-oriented programming]], a &amp;#039;&amp;#039;&amp;#039;class variable&amp;#039;&amp;#039;&amp;#039;  is a [[Variable (programming)|variable]] defined in a [[Class (programming)|class]] of which a single copy exists, regardless of how many [[Instance (computer science)|instance]]s of the class exist.&amp;lt;ref name=TheJavaTurotialVariables&amp;gt;{{cite web|url=http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|title=The Java Tutorial, Variables |accessdate=2010-10-21}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=TheJavaTutorialUnderstandingInstanceAndClassMembers&amp;gt;{{cite web|url=http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html|title=The Java Tutorial, Understanding Instance and Class Members|accessdate=2010-10-21}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=ThePythonLanguageReferenceCompoundStatements&amp;gt;{{cite web|url=https://docs.python.org/reference/compound_stmts.html#class-definitions|title=The Python Language Reference, Compound Statements|accessdate=2010-10-21}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=ObjectiveCRuntimeReference&amp;gt;{{cite web|url=https://developer.apple.com/documentation/objectivec/objective_c_runtime|title= Objective-C Runtime Reference |website=Apple Developer|accessdate=1 April 2018}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=ClassVariablesinCSharp&amp;gt;{{cite web|title=Class Variables in C#|url=https://syntaxdb.com/ref/csharp/class-var|website=Syntaxdb|accessdate=1 April 2018}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class variable is not an [[instance variable]]. It is a special type of [[Class (computer science)#Structure|class attribute]] (or class property, [[Field (computer science)|field]], or data member). The same dichotomy between &amp;#039;&amp;#039;instance&amp;#039;&amp;#039; and &amp;#039;&amp;#039;class&amp;#039;&amp;#039; members applies to [[method (computer science)|method]]s (&amp;quot;member functions&amp;quot;) as well; a class may have both [[instance method]]s and [[class method]]s.&lt;br /&gt;
&lt;br /&gt;
==Static member variables and static member functions==&lt;br /&gt;
{{see also|Static method}}&lt;br /&gt;
In some languages, class variables and class methods are either statically resolved, not via [[dynamic dispatch]], or their memory [[static memory allocation|statically allocated]] at compile time (once for the entire class, as [[static variable]]s), not dynamically allocated at run time (at every instantiation of an object). In other cases, however, either or both of these are dynamic. For example, if classes can be dynamically defined (at run time), class variables of these classes are allocated dynamically when the class is defined, and in some languages class methods are also dispatched dynamically.&lt;br /&gt;
&lt;br /&gt;
Thus in some languages, &amp;#039;&amp;#039;&amp;#039;static member variable&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;static member function&amp;#039;&amp;#039;&amp;#039; are used synonymously with or in place of &amp;quot;class variable&amp;quot; or &amp;quot;class function&amp;quot;, but these are not synonymous across languages. These terms are commonly used in [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]],&amp;lt;ref name=ClassVariablesinCSharp/&amp;gt; and [[C++]], where class variables and class methods are declared with the [[Static (keyword)|&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot; inline&amp;gt;static&amp;lt;/syntaxhighlight&amp;gt; keyword]], and referred to  as &amp;#039;&amp;#039;&amp;#039;static member variables&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;static member functions&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Order {&lt;br /&gt;
private:&lt;br /&gt;
    inline static int nextId = 0;&lt;br /&gt;
    int id;&lt;br /&gt;
public:&lt;br /&gt;
    Order():&lt;br /&gt;
        id{nextId++} {}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this C++ example, the class variable &amp;lt;code&amp;gt;Order::nextId&amp;lt;/code&amp;gt; is [[increment operator|incremented]] on each call to the [[constructor (computer science)|constructor]], so that &amp;lt;code&amp;gt;Order::nextId&amp;lt;/code&amp;gt; always holds the number of &amp;lt;code&amp;gt;Order&amp;lt;/code&amp;gt;s that have been constructed, and each new &amp;lt;code&amp;gt;Order&amp;lt;/code&amp;gt; object is given a &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; in sequential order. Since &amp;lt;code&amp;gt;nextId&amp;lt;/code&amp;gt; is a class variable, there is only one object &amp;lt;code&amp;gt;Order::nextId&amp;lt;/code&amp;gt;; in contrast, each &amp;lt;code&amp;gt;Order&amp;lt;/code&amp;gt; object contains its own distinct &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; field.&lt;br /&gt;
&lt;br /&gt;
Also note that the variable &amp;lt;code&amp;gt;Order::nextId&amp;lt;/code&amp;gt; is initialized only once (as &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot; inline&amp;gt;inline static&amp;lt;/syntaxhighlight&amp;gt;, it is initialized inside the class; prior to [[C++17]], it was only &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot; inline&amp;gt;static&amp;lt;/syntaxhighlight&amp;gt; and had to be initialized outside the class).&lt;br /&gt;
&lt;br /&gt;
A class with &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot; inline&amp;gt;static&amp;lt;/syntaxhighlight&amp;gt; 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.&amp;lt;ref&amp;gt;{{Cite web|title=Draft C++ Standard - Declarations and definitions|url=https://eel.is/c++draft/basic#def-5|quote=In the definition of an object, the type of that object shall not be an incomplete type ([basic.types.general]), an abstract class type ([class.abstract]), or a (possibly multidimensional) array thereof.|publisher=WG21|author=WG21|website=eel.is|date=23 April 2026}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Color {&lt;br /&gt;
    uint8_t r;&lt;br /&gt;
    uint8_t g;&lt;br /&gt;
    uint8_t b;&lt;br /&gt;
&lt;br /&gt;
    constexpr Color(uint8_t r, uint8_t g, uint8_t b) noexcept:&lt;br /&gt;
        r{r}, g{g}, b{b} {}&lt;br /&gt;
&lt;br /&gt;
    constexpr ~Color() = default;&lt;br /&gt;
&lt;br /&gt;
    static const Color BLACK;&lt;br /&gt;
    static const Color RED;&lt;br /&gt;
    static const Color GREEN;&lt;br /&gt;
    static const Color YELLOW;&lt;br /&gt;
    static const Color BLUE;&lt;br /&gt;
    static const Color MAGENTA;&lt;br /&gt;
    static const Color CYAN;&lt;br /&gt;
    static const Color WHITE;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
inline constexpr Color Color::BLACK = Color(0, 0, 0);&lt;br /&gt;
inline constexpr Color Color::RED = Color(255, 0, 0);&lt;br /&gt;
inline constexpr Color Color::GREEN = Color(0, 255, 0);&lt;br /&gt;
inline constexpr Color Color::YELLOW = Color(255, 255, 0);&lt;br /&gt;
inline constexpr Color Color::BLUE = Color(0, 0, 255);&lt;br /&gt;
inline constexpr Color Color::MAGENTA = Color(255, 0, 255);&lt;br /&gt;
inline constexpr Color Color::CYAN = Color(0, 255, 255);&lt;br /&gt;
inline constexpr Color Color::WHITE = Color(255, 255, 255);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    vertebrate_group: str = &amp;quot;mammals&amp;quot; # class variable&lt;br /&gt;
&lt;br /&gt;
dog: Dog = Dog()&lt;br /&gt;
print(dog.vertebrate_group) # accessing the class variable&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
In the above Python code, it does not provide much information as there is only class variable in the &amp;lt;code&amp;gt;Dog&amp;lt;/code&amp;gt; class that provide the vertebrate group of dog as mammals. In instance variable, one can customize the object (in this case, &amp;lt;code&amp;gt;dog&amp;lt;/code&amp;gt;) by having one or more [[instance variable]]s in the &amp;lt;code&amp;gt;Dog&amp;lt;/code&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
This can also be type hinted using {{Code|ClassVar}}.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from typing import ClassVar&lt;br /&gt;
&lt;br /&gt;
class Dog:&lt;br /&gt;
    vertebrate_group: ClassVar[str] = &amp;quot;mammals&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
{{Reflist|2}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Class Variable}}&lt;br /&gt;
[[Category:Object-oriented programming]]&lt;br /&gt;
[[Category:Variable (computer science)]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;/div&gt;</summary>
		<author><name>~2026-41267-59</name></author>
	</entry>
</feed>