반응형
시덥지 않은 기록 연달아서... ㅎ
static붙이면 정적필드, 정적메소드 라고 부른다.
두개 크로스 해서 짬뽕으로 보자.
public class Test {
public static void main(String[] args) {
Test2 t1 = new Test2();
Test2 t2 = new Test2();
t1.calc(100);
t2.calc(200);
int tot = Test2.getTotSum();
System.out.println(t1.sum);
System.out.println(Test2.tot_sum);
System.out.println(t2.sum);
System.out.println(tot);
}
Test2 t1 = new Test2();
Test2 t2 = new Test2();
t1.calc(100);
t2.calc(200);
int tot = Test2.getTotSum();
System.out.println(t1.sum);
System.out.println(Test2.tot_sum);
System.out.println(t2.sum);
System.out.println(tot);
}
}
class Test2{
int sum = 0;
static int tot_sum = 0;
void calc(int amt){
sum = sum + amt;
tot_sum = tot_sum + amt;
}
static int getTotSum(){
return tot_sum;
}
}
int sum = 0;
static int tot_sum = 0;
void calc(int amt){
sum = sum + amt;
tot_sum = tot_sum + amt;
}
static int getTotSum(){
return tot_sum;
}
}
뭐.. 이렇듯이..
클래스 자체에 속하는 뭐시깽이가 된다.
객체가 생길때 마다 값이 따라가느냐 안 따라가느냐의 차이. 그렇다능 ~
특정 객체에 속하는게 아니라 클래스 자체에 속함.
반응형
'TechNical > JAVA' 카테고리의 다른 글
interface는 뭐하는 놈일까? (0) | 2011.04.14 |
---|---|
super는 뭐 하는 놈일까 (0) | 2011.04.12 |
this란 뭐 하는 놈인가... (0) | 2011.04.12 |
java에서 실행된 디렉토리 찾기 getProperty (0) | 2010.06.29 |
Tomcat 설치하고 환경 설정하고 자바 실행시 그지 되는 경우 (0) | 2009.12.01 |
댓글