728x90
인스턴스(instance)
- new 키워드를 사용하여 인스턴스를 생성
- ex)
Student student = new Student();
- ex)
- 클래스는 객체의 속성을 정의, 기능 구현을 해놓은 공간
- 실제 클래스 기반으로 생성된 객체(인스턴스)는 각각 다른 멤버 변수 값을 가지게 된다.
- ex) Student 클래스에서 생성된 각각의 인스턴스는 각자 다른 학번, 이름, 주소를 가진다.
⁋힙 메모리
- 생성된 인스턴스는 동적 메모리(heap memory)에 할당된다.
- C와 C++에서는 사용자가 free() or delete를 이용하여 사용자가 해제시켜야 한다.
- JAVA에서는 Gabage Collector 가 주기적으로 사용하지 않는 메모리를 수거한다.
- 하나의 클래스로부터 여러 개의 인스턴스가 생성되고 각각 다른 메모리 주소(heap memory)를 가지게 된다.
public class StudentTest {
// 인스턴스 Lee와 Jeong은 다른 힙메모리 주소를 갖는다.
public static void main(String[] args) {
// 인스턴스 studentLee는 생성시 heap 메모리에 할당
Student studentLee = new Student();
studentLee.studentID = 12345;
studentLee.setStudentName("John");
studentLee.address = "Seoul";
studentLee.showStudentInfo();
// 인스턴스 studentJeong은 생성시 다른 heap 메모리에 할당
Student studentJeong = new Student();
studentJeong.studentID = 12123;
studentJeong.setStudentName("Tom");
studentJeong.address = "Busan";
studentJeong.showStudentInfo();
}
}
용어 정리
객체 : 객체 지향 프로그램의 대상, 생성된 인스턴스
클래스 : 객체를 프로그래밍 하기위해 코드로 정의해 놓은 상태
인스턴스 : new 키워드를 사용하여 클래스를 메모리에 생성한 상태
멤버 변수 : 클래스의 속성, 특성
메서드 : 멤버 변수를 이용하여 클래스의 기능을 구현한 함수
참조 변수 : 메모리에 생성된 인스턴스를 가리키는 변수
참조 값 : 생성된 인스턴스의 메모리 주소 값
728x90