package com.study.javastudy.study;

public class VariableScopeExam {
    int globalScope = 10; // 인스턴스 변수
    static int staticVal = 7; // 클래스 변수, static은 값을 공유 
    public void scopeTest(int value) {
        int localScope = 20; // 지역 변수

        System.out.println(globalScope);
        System.out.println(localScope);
        System.out.println(value);
    }

    public void scopeTest2(int value2) {
        System.out.println(globalScope);
        //System.out.println(localScope); //오류
        //System.out.println(value); // 오류
        System.out.println(value2);
    }


    public static void main(String[] args) {
        // 모든 클래스는 인스턴스화 하지 않은 상태로 사용할 수 없다
        // static을 사용하면 인스턴스화 하지 않아도 사용 가능
//        System.out.println(globalScope); //오류
//        System.out.println(localScope); //오류
//        System.out.println(value); //오류
        System.out.println(staticVal); // 7, 클래스 변수는 인스턴스화 하지 않고 사용 가능

        VariableScopeExam v1 = new VariableScopeExam();
        System.out.println(v1.globalScope); // 10

        VariableScopeExam v2 = new VariableScopeExam();
        v1.globalScope = 10;
        v2.globalScope = 20;
        System.out.println(v1.globalScope); // 10
        System.out.println(v2.globalScope); // 20

        v1.staticVal = 50;
        v2.staticVal = 100;
        System.out.println(v1.staticVal); // 100
        System.out.println(v2.staticVal); // 100
        System.out.println(VariableScopeExam.staticVal); // 100
    }
}

'자바 기초' 카테고리의 다른 글

생성자  (0) 2024.01.27
열거형(enum)  (0) 2024.01.27
String 클래스 메소드  (0) 2024.01.27
메소드 사용  (0) 2024.01.27
메소드 선언  (0) 2024.01.27

+ Recent posts