프로그래밍
static VS non-static
주멘이
2018. 6. 18. 14:09
static - 변수, 함수가 class에 종속
non static - 변수, 함수가 class가 아닌 object(instance)에 종속
1. static이 붙은 변수, 메소드를 instance 없이 사용가능한 이유
- instance의 모태가 되는 class가 메모리에 올라갈 때 class의 member로 메모리에 같이 올라가기 때문
2. static 메소드에서 instance 변수를 사용할 수 없는 이유
- static 사용 시점에 instance가 생성되어 있는지 알 수 없기 때문
3. static의 사용 이유
- instance를 생성하지 않고 사용할 수 있기 때문에 속도가 빠르다
- class 설계 시 멤버 변수 중에 모든 instance에서 공통으로 사용하는 부분을 static으로
- class 설계 시 메소드 중 instance 변수를 사용하지 않는 부분을 static으로