[JAVA] 정적 변수와 메소드


[JAVA] 정적 변수와 메소드

static class Counter { int count = 0; Counter() { this.count++; System.out.println(this.count); } } public class HelloSpringApplication { public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); } } 다음과 같은 코드가 있다고 가정할때 위 코드는 다음과 같은 결과값이 나온다. 1 1 서로다른 객체를 생성하여 다른 객체를 가르키고 있기 때문에 발생하는 결과이다. 다음 예제를 살펴보자 class Counter { static int count = 0; Counter() { count+..


원문링크 : [JAVA] 정적 변수와 메소드