Member variable

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

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 languages, these are distinguished into two types: if there is only one copy of the variable shared with all instances of the class, it is called a class variable or static member variable; while if each instance of the class has its own copy of the variable, the variable is called an instance variable.[1]

Examples

Java

class Program
{
    static void Main()
    {
    	// This is a local variable. Its lifespan
    	// is determined by lexical scope.
    	Foo foo;
    }
}

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;
}

C++

#include <iostream>
class Foo {
    int bar; //Member variable
  public:
    void setBar (int newBar) {bar = newBar;}
};

int main () {
  Foo rect; //Local variable
  return 0;
}

References

<templatestyles src="Reflist/styles.css" />

Cite error: Invalid <references> tag; parameter "group" is allowed only.

Use <references />, or <references group="..." />

See also


<templatestyles src="Asbox/styles.css"></templatestyles>

  1. Lua error in package.lua at line 80: module 'strict' not found.