
: 정적인(고정되어), 공유가 되는, 클래스 이름으로 접근 가능(=객체 생성 없이)
| static 메소드 | static 없는 메소드 or 생성자 |
|---|---|
| 그냥 변수명으로 호출, class명.변수명으로 호출 | this로 호출할 수 있음 |
class Ex{
int a; // 멤버
static int b; // 멤버위장, static 변수, 정적 변수, 클래스 변수
public void setA(int a){ // 멤버
int c = c; // 지역변수
this.a = a + c;
}
public static void setB(int b){ // 멤버 위장 // static메소드, 정적메소드
this.b = b;
}
public static void md1( ){ // 멤버 위장
System.out.println("md1호출");
}
public void md2( ){ // 멤버
System.out.println("md2호출");}
}
class ExSub{
int a; // 멤버
static int b; // 멤버위장, static 변수, 정적 변수, 클래스 변수
public ExSub() {
this.b = 5; // (X)
}
public void setA(int a){ // 멤버
int c = 5; // 지역변수
this.a = a + c;
this.b = 5;
}
public static void setB(){ // 멤버 위장 // static메소드, 정적메소드
b = 5;
a = 2; // static 메소드 안에는 static이 없는 멤버변수는 호출할 수 없음.
this.a = 2; // 안됨
}
public static void setB(int paramB){ // 멤버 위장 // static메소드, 정적메소드
b = paramB;
}
public static void md1( ){ // 멤버 위장
System.out.println("md1호출");
}
public void md2( ){ // 멤버
System.out.println("md2호출");}
}
메모리 영역
Method = Static = Class = 정적
static
Ex
static int b;
public static void setB(int b){
this.b = b;
}
public static void md1( ){
System.out.println("md1호출");
}
class Ex{
int a;
static int b;
public static void setB( ){
int c = b+5; // 지역변수
// this.b = b; //(X) static 키워드가 붙어있는 변수에서는 this로 접근 할 수 없음
Ex.b = b;
this.b = c; // static 키워드가 붙어있는 변수에서는 this로 접근 할 수 없음
System.out.println(b+c); //(O) static 메소드에서는 static 메소드 , static 멤버변수만 호출 가능함
System.out.println(a+b+c);//(X)
md1( );(X)
md2( ); (O)//
}
public voic setA(int a){
this.a = a;
System.out.println(a);
}
public void md1( ){ System.out.println(a);}
public static void md2( ){System.out.println(b);}
}