Member variable

Variable associated with a specific object, and accessible for all its methods


title: "Member variable" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["object-oriented-programming", "variable-(computer-science)", "articles-with-example-python-(programming-language)-code"] description: "Variable associated with a specific object, and accessible for all its methods" topic_path: "technology/programming-languages" source: "https://en.wikipedia.org/wiki/Member_variable" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Variable associated with a specific object, and accessible for all its methods ::

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.

Examples

C++

::code[lang=cpp]

class Foo { int bar; // Member variable public: void setBar(const int newBar) { bar = newBar; } };

int main () { Foo rect; // Local variable

return 0; } ::

Java

::code[lang=java] 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;

} ::

Python

::code[lang=python] 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) ::

Common Lisp

::code[lang=lisp] (defclass foo () (bar))

(defvar f (make-instance 'foo))
(setf (slot-value f 'bar) 100) (print (slot-value f 'bar)) ::

Ruby

::code[lang=ruby] /* 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 < Dog

The "class instance variable" is defined within the class body with a single at-sign

and describes data about only the Poodle class. It makes no claim about its parent class

or any possible subclass derived from Poodle

@sheds = false

When a new Poodle instance is created, by default it is untrained. The 'trained' variable

is local to the initialize method and is used to set the instance variable @trained

An instance variable is defined within an instance method and is a member of the Poodle instance

def initialize(trained = false) @trained = trained end

def has_manners? @trained end

end

p = Poodle.new p.class.sheds #=> false p.has_manners? #=> false ::

PHP

::code[lang=php]

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; :: ### Lua ::code[lang=lua] --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 ## References 1. Richard G. Baldwin. (1999-03-10). ["Q - What is a member variable?"](http://www.dickbaldwin.com/java/Java020.htm). *Richard G Baldwin Programming Tutorials*. ::callout[type=info title="Wikipedia Source"] This article was imported from [Wikipedia](https://en.wikipedia.org/wiki/Member_variable) and is available under the [Creative Commons Attribution-ShareAlike 4.0 License](https://creativecommons.org/licenses/by-sa/4.0/). Content has been adapted to SurfDoc format. Original contributors can be found on the [article history page](https://en.wikipedia.org/wiki/Member_variable?action=history). ::
object-oriented-programmingvariable-(computer-science)articles-with-example-python-(programming-language)-code