자바의 정석 - 기초편

선언 위치에 따른 변수의 종류 / 클래스 변수와 인스턴스 변수

ODaram 2022. 8. 4. 13:45

선언 위치에 따른 변수의 종류

Class Variables                                                                                                                        

{    -> 클래스 시작                                                                                                                    

    int iv;                    // 인스턴스 변수 (instance variable)                                                         

                                                                                                 > 1. 클래스 영역 (선언문만 가능)

    static int cv;         // 클래스 변수 (static 변수, 공유 변수)                                                      

                                                                                                                                                 

    void method()                                                                                                                       

    {     -> 메서드 시작                                                                                                                

        int Lv = 0;          // 지역 변수(local variable)                            <= 2. 메서드 영역              

     }    -> 메서드 끝                                                                                                                    

}    -> 클래스 끝                                                                                                                         

                 

영역 

1. 클래스 영역

   1) iv

   2) cv  (= static + iv)

2. 메서드 영역

   1) Lv

☆ 객체 = iv를 묶어 놓은 것.

 

클래스 변수(cv)와 인스턴스 변수(iv)

선언위치 영역 - 클래스 영역 : iv, cv

                        - 메서드 영역 : lv

 

카드 예

카드 중 공통적으로 유지되어야하는 값에 static을 붙임

 

                                                Card c = new Card();     // 객체 생성

                                                c.kind = "Heart";

                                                c.number = 5;

                                                c. width = 200; (권장안함)  -> Card.width = 200;  (클래스 이름 붙여주기)

                                                c.height = 300; (권장안함)  -> Card.height = 300;

 

클래스 변수와 인스턴스 변수 - 예제

class Ex6_3 {
	public static void main(String args[]) {
		System.out.println("Card.width = " + Card.width);
		System.out.println("Card.height = " + Card.height);

		Card c1 = new Card();
		c1.kind = "Heart";
		c1.number = 7;

		Card c2 = new Card();
		c2.kind = "Spade";
		c2.number = 4;

		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")");
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")");
		System.out.println("c1은 width와 height를 각각 50, 80으로 변경합니다.");
		c1.width = 50;		//c1 -> 참조변수, cv
		c1.height = 80;

		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")");
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")");
	}
}

class Card {
	String kind;
	int number;
	static int width = 100;
	static int height = 250;
}

c1.width = 50;

c1.height = 80;

> c1=참조변수, iv같지만 cv임

> 헷갈리게 만드는 코드라 

   Card.width = 50;

   Card.height = 80; 

   과 같이 변경해주는 것이 좋음

예제 결과 값

'자바의 정석 - 기초편' 카테고리의 다른 글

메서드 호출  (0) 2022.08.04
메서드란? / 메서드의 선언부와 구현부  (0) 2022.08.04
클래스의 정의  (0) 2022.08.04
객체 배열  (0) 2022.08.04
객체의 생성과 사용  (0) 2022.08.03