반응형
선언위치에 따른 종류
영역 1. 클래스 영역 : iv(instance variable), cv(class variable)
2. 메서드 영역 : lv(local variable)
![](https://blog.kakaocdn.net/dn/xVtdU/btsF2SgTK2R/JvtYv7S1ts0eUgdSG6h9Lk/img.png)
무늬와 숫자같이 카드마다 개별적으로 달라야 하는 것은 인스턴스 변수(iv)를 사용한다.(개별 속성)
폭, 높이와 같이 카드마다 개별적으로 같아야 하는 것은 클래스 변수(cv)를 사용한다.(공통 속성)
![](https://blog.kakaocdn.net/dn/ElFrE/btsF5vd6SwW/noKgMhseKwqnQ6xquBSU3K/img.png)
class Ex6_3Ex {
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.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;
}
![](https://blog.kakaocdn.net/dn/cT46HG/btsF3C5VhtY/pb1rQ4UFjMNWJPeP11uOa1/img.jpg)
반응형
'java↗' 카테고리의 다른 글
java↗7-9. 메서드호출 (0) | 2024.03.27 |
---|---|
java↗7-8. 메서드란? 메서드의 선언부와 구현부 (0) | 2024.03.25 |
java↗7-6. 선언위치 변수종류 (0) | 2024.03.25 |
java↗7-5. 클래스의 정의 (0) | 2024.03.24 |
java↗7-4. 객체 배열 (0) | 2024.03.24 |