필드와 변수의 정의

필드와 변수의 정의

자바 문서 정리

 

📕 서론


욕심 많았던 첫 프로젝트를 끝내고 다음 진도로 나아가기 전 기초 회귀 학습을 진행하고 있다.

이번 포스팅에는 자바의 변수와 필드에 대해 알아볼 것이다.

 

📕 자바 변수 종류에 대한 정의


The Java programming language defines the following kinds of variables.

자바 프로그래밍 언어의 변수 종류에 대해 다음과 같이 정의한다.

 

  • Instance Variables (NonStatic Fields) Technically speaking, objects store their individual states in "nonstatic fields", that is, fields declared without the static keyword. Nonstatic fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

    • 인스턴스 변수(비정적 필드)는 엄밀히 따지면 오브젝트에 저장된 각각의 상태다. 이는 정적 키워드(static) 없이 선언된 필드인 인스턴스 변수(비정적 필드)에 각각의 상태를 저장한다. 정적 필드가 아닌 필드는 각 클래스의 인스턴스마다(또는 각 객체 마다라고 부를 수 있겠다.) 고유하기 때문에 인스턴스 변수라고 부른다. 한 자전거의 현재 속도는 다른 자전거의 현재 속도와 독립적인 것이다.

 

  • Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

    • 클래스 변수(정적 필드)는 정적 수정자(static)를 사용해 선언된 필드이며, 클래스가 인스턴스화 됐는지에 관계없이 이 변수가 정확히 한 개만 존재함을 컴파일러에게 알려준다. 개념적으로 모든 자전거에는 동일한 수의 기어가 존재하므로 특정 자전거의 기어 수를 정의하는 필드는 정적(static)으로 표시할 수 있다. static int numGears = 6; 코드는 이러한 정적 필드를 생성하는 예시이다. 또한 키워드 final을 추가해 이 값이 절대로 변하지 않는 상수임을 표시할 수도 있다.

 

  • Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

    • 지역 변수는 인스턴스 변수와 유사하지만 메서드가 상태를 저장하는 곳을 의미한다. 지역 변수를 선언하는 문법은 필드를 선언하는 문법과 유사하다(예: int count = 0;). 변수를 지역 변수로 지정하는 키워드는 따로 존재하지 않으며, 이 판단은 전적으로 변수가 선언된 위치에 근거한다. 따라서 지역 변수는 메서드 안에 있는 변수를 의미하며 지역 변수는 해당 메서드에서만 접근할 수 있고 메서드 밖에서는 지역 변수에 접근할 수 없다.

 

  • Parameters You've already seen examples of parameters, both in the Bicycle class and in the main method of the "Hello World!" application. Recall that the signature for the main method is public static void main(String\[\] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameteraccepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.

    • 당신은 이미 자전거 클래스에서 매개변수를 보았다. 메인 메서드는 문법상 public static void main(String[] args)로 표기하는데, 여기서 args변수를 매개변수라고 부른다. 반드시 기억해야 할 것은 매개변수는 항상 필드가 아닌 변수로 분류된다는 것이다. 이 내용은 나중에 튜토리얼에서 알게 될 다른 매개변수 구성(생성자나 예외처리)에도 모두 동일하게 적용된다.

 

📕 결론


  • 필드는 상태가 저장되는 곳을 말한다. (Stack Area)

    • 객체의 상태가 저장되는 곳은 필드로 분류한다.
    • 필드는 모두 변수라고 부를 수도 있다.
    • 다만 모든 변수를 필드라고 부를 수 있는 것은 아니다.

      ex. 매개변수

 

  • 인스턴스 변수 = 비정적 필드 (Heap Area를 참조하는 개체)

    • 인스턴스 변수는 각 클래스의 인스턴스마다 독립적으로 존재한다. (당연한 소리다.) 인스턴스가 뭔지 아직 잘 모른다면 이를 먼저 이해해야 할 것이다.

 

  • 클래스 변수 = 정적 필드 (Method Area를 참조하는 개체)

    • static을 선언해 정적 멤버임을 컴파일러에게 알려준다. 이는 JVM에 의해 메모리에 적재될 때 가장 먼저 적재된다. JVM에 대한 이해 역시 필요하다.

 

  • 지역 변수는 메서드 안에 위치한 필드를 말한다.

    • 메서드는 일반적인 함수와 다르게 클래스에 종속된 함수를 의미하며 클래스 함수라고도 부른다.
    • 컴파일러는 메서드에 종속된 필드를 지역 변수로 판단한다.

 

  • 매개변수는 절대로 필드가 될 수 없다.

    • 매개변수는 무조건 변수로 분류된다.

© 2022. All rights reserved.