[Java] 10.3.2. 생성자 this 의미

김주희's avatar
Feb 26, 2025
[Java] 10.3.2. 생성자 this 의미
Contents
1. this

1. this

  1. 현재 객체 자신을 가리키는 참조 변수이다.
  1. 컴파일러에서 자동으로 생성한다.
  1. this를 사용하지 않으면 컴파일러는 둘 다 매개 변수로 생각하게 된다.(문법 규칙에 따라서)
package ex04; class Cat { private String name; private String color; public String getName() { return name; } public String getColor() { return color; } // 디폴트 생성자 생략되어있음 // 생성자도 stack이 있다. // 자기를 호출한 상위 stack을 찾을 수 있다. // stack에서 heap을 찾고 싶으면 this 사용 public Cat(String name, String color) { // 얘도 메서드( (){} ) ->Cat 생성자 stack 생김 this.name = name; //this는 heap 변수 (heap을 가리킴) // 그냥 name 쓰면 생성자 stack에 있는 name을 찾음. heap에 있는 걸 쓰고 싶을때 this 사용 this.color = color; // 굳이 name = n이라고 하기에는 귀찮으니까 } } public class CatTest { public static void main(String[] args) { Cat cat = new Cat("먼지", "검정색"); System.out.println(cat.getName()); System.out.println(cat.getColor()); } }
notion image
Share article

jay0628