반응형
static
static이 붙은 변수들은 객체들이 다 같이 공유하는 데이터를 의미한다.
static이 붙은 메소드들은 객체들의 데이터와 관계없는 완벽하게 공통적인 로직을 정의할 때 사용한다.
따라서 static 메소드에서는 인스턴스 변수나 객체의 메소드를 사용할 수 없다.

public class StaticVar
{
static int a = 1;
int b =1;
void staticTest()
{
a = a + 1;
b = b + 1;
System.out.println("static int a : "+a);
System.out.println("member int b : "+b);
}
}
public class StaticMain
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
StaticVar v1 = new StaticVar();
v1.staticTest();
StaticVar v2 = new StaticVar();
v2.staticTest();
v2.staticTest();
}
}

반응형
'Language > JAVA' 카테고리의 다른 글
| JAVA - this로 사용하는 멤버변수 (0) | 2022.07.01 |
|---|---|
| JAVA - method overroading (0) | 2022.07.01 |
| JAVA - 클래스와 객체(인스턴스) (0) | 2022.07.01 |
| JAVA - 배열(Array) 생성 (0) | 2022.07.01 |
| JAVA - 반복문 (0) | 2022.07.01 |