Programming/JAVA

static 기능

dev? 2022. 5. 4. 12:35
반응형

static 

- 프로그램 시작시, 메모리에 생성되며 프로그램 종료 시, 메모리에서 없어진다. 

- 클래스와 상관없이 독립적으로 사용 가능하다. 

 

static 변수 static 메소드 일반 메소드
- 모든 객체가 공통으로 사용하는
  변수가 필요한 경우
- 변수에 static 붙여서 사용
- static 변수 전용 메소드 
- 반드시 static 변수만 사용 가능하며, 
static 메소드만 호출 가능
- static 변수와 일반 변수 모두
사용 가능
- 일반 메소드와 static 메소드 모두
사용 가능 

 

class A1 {
	int method1(int x, int y) {
		return x+y;
	}
}

/* 객체 생성 후 메소드 호출 */
A1 a1 = new A1();
a1.method1(5, 7);
/* static 사용한 경우 */
class A1 {
	static int method1(int x, int y) {
		return x+y;
	}
}

A1.method1(5, 7);

 

반응형