
package com.haenin.section06.statickeyword;
public class Application {
public static void main(String[] args) {
/* 목표, static 키워드에 대해 이해할 수 있다. */
/* 설명.
* static
* : 프로그램이 실행될 때 정적 메모리 영역(static 영역 또는 클래스 영역)에 할당하닌 키워드이다.
* (대표적인 예로 싱글톤(singleton) 객체 또는 설정값(DB 연결 정보)를 담을 용도로 쓸 수 있다.*/
StaticTest st1 = new StaticTest();
/* 설명. 현재 두 필드가 가지고 있는 값 확인 */
System.out.println("non-static field: " + st1.getNonStaticCount());
System.out.println("static field: " + StaticTest.getStaticCount());
/* 설명. 각 필드 값들을 증가 */
st1.increadNonStaticCount();
StaticTest.increaseStaticCount();
/* 설명. 두 필드 값 확인 */
System.out.println("non-static field: " + st1.getNonStaticCount());
System.out.println("static field: " + StaticTest.getStaticCount());
/* 설명. 새로운 객체 생성 후 적용 */
StaticTest st2 = new StaticTest();
System.out.println("non-static field: " + st2.getNonStaticCount());
System.out.println("static field: " + StaticTest.getStaticCount());
}
}
package com.haenin.section06.statickeyword;
public class StaticTest {
private int nonStaticCount;
private static int staticCount;
public StaticTest() {
}
public int getNonStaticCount() {
return this.nonStaticCount;
}
public static int getStaticCount() {
return StaticTest.staticCount;
}
public int increadNonStaticCount() {
return nonStaticCount++;
}
public static int increaseStaticCount() {
return staticCount++;
}
}
