출처 :
권지현 블로그
https://dev-9rm.tistory.com/87
package day0602;
class Apple {
private int num;
Apple() {
this.num = 1;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
public class Test01 {
public static void main(String[] args) {
Apple apple = new Apple();
Apple copy = apple;
System.out.println(apple);
System.out.println(copy);
String a = new String();
a = "a값";
String b = a;
System.out.println(a);
System.out.println(b);
// Apple copy=new Apple();
// copy.setXxx(apple.getXxx());
//new개수==객체개수
}
}
출력 결과 :
day0524.Apple@7c30a502
day0524.Apple@7c30a502
a값
a값
Apple 클래스 객체를 "apple"이라는 이름으로 선언하고 new(인스턴스화, 객체화) 하였다
그 후 Apple 클래스 객체 자료형을 갖는 "copy"를 선언하여 "apple"을 저장하였다
이때 copy는 new 하지 않았기 때문에 새로운 객체가 아니라 일종의 바로가기 형식으로 main()에 생성되며 그 자리에 "apple"의 객체 주소를 가질 분이다
해당 주소를 가지고 있기 때문에 참조하여 값에 접근할 수 있다
※ 힙메모리에 copy는 존재하지 않는다
단, copy는 객체는 맞다
우리가 쓰는 컴퓨터 바로가기 아이콘 또한 이러한 방식으로 구현된다