본문으로 이동

멤버 변수

위키백과, 우리 모두의 백과사전.

객체 지향 프로그래밍에서 멤버 변수(member variable) 또는 멤버 필드는 특정 객체와 연결된 변수의 하나이며, 해당 변수의 모든 메소드(멤버 함수)에 접근이 가능하다. 클래스 기반 언어에서 이들은 두 종류로 구별된다: 모든 인스턴스의 클래스와 공유되는 변수의 사본이 하나만 있을 경우 이를 클래스 변수나 정적 멤버 변수로 부른다. 클래스의 각 인스턴스가 자신만의 변수 복사본을 소유하고 있는 경우 해당 변수는 인스턴스 변수라 부른다.[1]

예제[편집]

C++[편집]

#include <iostream>
class Foo {
    int bar; // 멤버 변수
  public:
    void setBar (const int newBar) { bar = newBar; }
};

int main () {
  Foo rect; // 지역 변수

  return 0;
}

자바[편집]

class Program
{
    static void main(final String arguments[])
    {
    	// 이것은 로컬 변수이다. 이 변수의 수명은
    	// 어휘 범위에 의하여 결정된다.
    	Foo foo;
    }
}

class Foo
{
    // 이것은 멤버 변수이다. 이 변수의 새로운
    // 인스턴스는 Foo의 각각의 새로운
    // 인스턴스들을 위해 만들어진다. 이 변수의
    // 수명은 Foo의 "this" 인스턴스의 수명과
    // 동일하다.
    int bar;
}

같이 보기[편집]

각주[편집]

  1. Richard G. Baldwin (1999년 3월 10일). “Q - What is a member variable?”. http://www.dickbaldwin.com/: Richard G Baldwin Programming Tutorials. 2011년 8월 12일에 확인함. A member variable is a member of a class (class variable) or a member of an object instantiated from that class (instance variable). It must be declared within a class, but not within the body of a method of the class.