encyclopegia.org

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 are distinguished into two types: class variables (also called static member variables), where only one copy of the variable is shared with all instances of the class; and instance variables, where each instance of the class has its own independent copy of the 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 are distinguished into two types: class variables (also called static member variables), where only one copy of the variable is shared with all instances of the class; and instance variables, where each instance of the class has its own independent copy of the variable. class Foo { int bar; // Member variable public: void setBar(const int newBar) { bar = newBar; } }; int main () { Foo rect; // Local variable return 0; } public class Program { public static void main(String[] args) { // This is a local variable. Its lifespan // is determined by lexical scope. Foo foo; } } public class Foo { /* This is a member variable - a new instance of this variable will be created for each new instance of Foo. The lifespan of this variable is equal to the lifespan of "this" instance of Foo */ int bar; } class Foo: def __init__(self): self._bar = 0 @property def bar(self): return self._bar @bar.setter def bar(self, new_bar): self._bar = new_bar f = Foo() f.bar = 100 print(f.bar) (defclass foo () (bar)) (defvar f (make-instance 'foo)) (setf (slot-value f 'bar) 100) (print (slot-value f 'bar)) /* Ruby has three member variable types: class, class instance, and instance. / class Dog # The class variable is defined within the class body with two at-signs # and describes data about all Dogs *and* their derived Dog breeds (if any) @@sniffs = true end mutt = Dog.new mutt.class.sniffs #=> true class Poodle false p.has_manners? #=> false foo = $foo; } } // Create a new Example object. // Set the "foo" member variable to 5. $example = new Example(5); // Overwrite the "foo" member variable to 10. $example->foo = 10; // Prints 10. echo $example->foo; --region example --- @class example_c --- @field foo number Example "member variable". local example_c = {} local example_mt = {__index = example_c} --- Creates an object from example. --- @return example_c function example_c.new(foo) -- The first table argument is our object's member variables. -- In a Lua object is a metatable and its member variables are table key-value pairs. return setmetatable({ foo = foo }, example_mt) end --endregion -- Create an example object. -- Set the "foo" member variable to 5. local example = example_c.new(5) -- Overwrite the "foo" member variable to 10. example.foo = 10 -- Prints 10. print(example.foo)

References

No reference rows published for this article.

Cite this article

MLA

"Member variable." encyclopegia.org, 2025, https://encyclopegia.org/wiki/member-variable. Accessed 24 November 2026.

Chicago

"Member variable." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/member-variable.

APA

encyclopegia.org. (2025). Member variable. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/member-variable

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Hyperbolic 3-manifold

enwiki · references available

Rote Flora

enwiki · references available

Man lebt nur einmal!

enwiki · references available

Field (sculpture)

enwiki · references available

Wiener Frauen

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1317280864 · 0.5 · snapshot enwiki-001317280864

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.