Language/Java
[JAVA 11] 자바 변수(Variable) ③ static (소속/소유) 유무
reifier.tistory.com 으로 이전하였습니다.
2018. 2. 4. 20:43
반응형
3. 소속(static(고정된)) 유무
<1> 클래스 소속 변수(=정적변수)
ex) static int t = 1;
<2> 객체 소속 변수(=동적변수)
ex) int i =1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class VS6{ int i; // 객체소속 멤버변수 static int j; // 클래스소속 멤버변수 void m1(){ System.out.println(" m1() i : " + this.i); } static void m2(){ System.out.println(" static m2() j : " + VS6.j); } } class VS6User{ public static void main(String[] args){ VS6 vs = new VS6(); // int i; 를 사용하기 위해서는객체를 만들어 줘야 한다. System.out.println(" i : " + vs.i); System.out.println(" j : " + VS6.j); // 클래스소속이기에 클래스만 써주고 뒤의 멤버변수를 이용하면 된다. vs.m1(); // 객체.메소드(); VS6.m2(); // 클래스.메소드(); } } // static 메소드 안에서만 static 멤버를 객체생성없이 호출 가능!! | cs |
반응형