static과 JVM 메모리 모델

강9·2023년 12월 4일
0

Java

목록 보기
53/71
post-thumbnail

🔖 static과 memory의 관계

public class StaticTest {
	public static void main(String[] args) {
    	int a = 10;
        int b = 20;
        int sum = StaticTest.hap(a,b);
        System.out.println(sum);
    }
    public static int hap(int a, int b) {
    	int v = a+b;
        return v;
    }
}

메인(시작)클래스가 동작(실행)되는 방식

  1. JVM이 실행할 클래스를 찾는다.
  2. static 키워드가 붙어있는 멤버들은 정해진 메모리(static-zone) 위치에 한번 자동으로 로딩한다.
    -> static 멤버들은 클래스를 사용하는 시점에서 딱 한번 메모리에 로딩 된다는 점이 중요
    -> 여기서는 main() 메서드가 static이기 때문에 메모리에 자동으로 로딩한다.
  3. JVM이 static-zone에서 main()메서드를 호출한다.
  4. 호출된 메서드를 Call Static Fame Area(Stack Area)에 push(기계어코드를 넣고) 한 뒤 동작을 시작한다.

Call Static Fame Area(Stack Area)

  • 메서드가 호출되면 호출된 기계어코드가 push되고 실행되는 메모리공간
  • 현재 프로그램이 실행되고 있는 상태를 파악할 수 있다.
  • LIFO(Last-In-First-Out) 구조이다.

PC

  • 현재 수행중인 프로그램 위치

프로그램 종료

  • stack에 아무것도 없으면 프로그램이 종료된다.

🔖 static과 none static 멤버들의 접근 방법

public class NoneStaticTest {
	public static void main(String[] args) {
    	int a = 10;
        int b = 20;
        NoneStaticTest st = new NoneStatictest();
        int sum = st.hap(a,b);
        System.out.println(sum);
    }
    public int hap(int a, int b) {
    	int v = a+b;
        return v;
    }
}

Method Area

  • 메서드의 기계어 코드가 할당되는 메모리 공간
  • static 멤버들의 할당되는 메모리 공간

Heap Area

  • 객체가 생성(new)되는 메모리 공간

🔖 두 개의 클래스로 static 메서드 접근

✅ static 멤버 접근 방법

클래스명.호출메서드 (ex. MyUtil.hap)


Static 접근 방법

public class StaticAccess {
	public static void main(String[] args) {
    	int a = 10;
        int b = 20;
        int sum = MyUtil.hap(a,b);
        System.out.println(sum);
    }
}
public class MyUtil {
    	public static int hap(int a, int b) { // 클래스 메서드
        int v = a+b;
        return v;
        }
	}

static 멤버는 클래스를 사용하는 시점에서 자동으로 static-zone에 로딩된다.
따라서 new를 이용해서 객체를 생성할 필요가 없다.

None Static 접근 방법

public class NoneStaticAccess {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        // MyUtil1
        // 객체 생성
        MyUtil1 my1 = new MyUtil1();
        int sum = my1.hap(a,b);
        System.out.println("sum = " + sum);
    }
}
public class MyUtil1 {
    public int hap(int a, int b) {
        int v = a+b;
        return v;
    }
}

🔖 class(객체)의 모든 멤버가 static 멤버일 경우

public class AllStaticTest {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
//        AllStatic st = new AllStatic();
//        System.out.println(st.hap(a,b));
//        System.out.println(st.max(a,b));
//        System.out.println(st.min(a,b));

        System.out.println(AllStatic.hap(a,b));
        System.out.println(AllStatic.max(a,b));
        System.out.println(AllStatic.min(a,b));
    }
}
public class AllStatic {
    private AllStatic() { // private 생성자 생성
    }

    public static int hap(int a, int b) { // 총합
        int v = a+b;
        return v;
    }

    public static int max(int a, int b) { // 최대값
        return a > b ? a : b;
    }

    public static int min(int a, int b) { // 최소값
        return a < b ? a : b;
    }
}

class의 이름으로 static 멤버들을 호출하기 위해
private 생성자를 만들어 객체생성을 하지 못하도록 막는다.


🔖 class, object, instance의 상호관계

Class(클래스) : 객체를 모델링하는 도구(설계도)
Object(객체) : class를 통해서 선언되는 변수
Instance(인스턴스, 실체) : 객체생성에 의해 메모리(Heap Memory)에 만들어진 객체

-> ClassObject는 서로 비슷한 개념(객체를 나타내는 용어)

📌 Class

객체를 모델링하는 도구(설계도)

public class Student {
	private String name;
    private String dapt;
    private int age;
    private String email;
    private int year;
    private String phone;

	public Student() {
    }
    // 이하 생략
}

📌 Object

class를 통해서 선언되는 변수

  • 변수가 구체적인 실체(대상)를 가리키지 않는 상태(객체 변수)
  • 객체가 서로 구분이 되지 않는 시점
Student st;

📌 Instance

객체생성에 의해 메모리(Heap Memory)에 만들어진 객체

  • 객체가 구체적인 실체를 가리키는 상태(인스턴스 변수)
  • 객체가 서로 구분이 되는 시점
st = new Student();
profile
코린이 일기

0개의 댓글