static이라는 키워드가 메소드 앞에 붙으면 이 메소드는 클래스 메소드(static method)가 된다.

무슨 말인지 알쏭달쏭하겠지만 예제를 보면 매우 쉽다.

public class Counter { static int count = 0; Counter() { this.count++; } public static int getCount() { return count; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println(Counter.getCount()); } }

getCount() 라는 static 메소드가 추가되었다. main 메소드에서 getCount() 메소드는 Counter.getCount() 와 같이 클래스를 통해 호출할 수 있게 된다.


클래스 메소드 안에서는 인스턴스 변수 접근이 불가능 하다.

위 예에서 count는 static 변수이기 때문에 클래스 메소드(static method)에서 접근이 가능한 것이다.






객체 생성이 안되었으니까 접근이 안된다는 말 맞죠?

객체 생성하면 되는거 맞죠?