Static

박산해·2025년 4월 29일

public class Exstatic {

public static void main(String[] args) {
    /*
     * 1. static이란 무엇인가?
     *      클래스에 고정된 고용 저장공간
     * 2. static 변수 (field)
     *      모든 객체가 '공유'하는 값
     *      객체를 만들지 않고 클래스 이름으로 접근
     * 3. static 메서드 (method)
     *      객체 없이 호출하는 함수
     *      static 변수만 접근 가능
     * 4. static final 상수(constant)
     *      변하지 않는 고정값
     *      대문자로 작성하는 것이 관례
     */
    StaticDemo a = new StaticDemo();
    StaticDemo.showStaticInfo();

    StaticDemo b = new StaticDemo();
    StaticDemo.showStaticInfo();

    StaticDemo c = new StaticDemo();
    StaticDemo.showStaticInfo();

    int a1=56,b1=14;
    System.out.println(Math.max(a1, b1));

}

}

//staticDemo
class StaticDemo{
//1. static 변수(모든 객체가 공유)
static int staticCounter = 0;

//2. 일반 인스턴스 변수(객체마다 별도로 존재)
int instanceCounter = 0;

//3. static final 상수(절대 변하지 않는 값)
static final int MAX_VALUE=100;

//생성자(Constructor)
StaticDemo(){
    staticCounter++;
    instanceCounter++;

}

//4. static메서드(객체 없이 호출 가능)
static void showStaticInfo(){
    System.out.println("static 변수 : "+staticCounter);
    System.out.println("최대값 상수 : "+MAX_VALUE);
}

//5. 일반 인스턴스 메서드
void showInstanceInfo(){
    System.out.println("instance 변수 : "+instanceCounter);
}

}

0개의 댓글