# 대마법사 메서드 부분에서 name, hp,mp 를 불러올때 this가 필요한 이유가 궁금합니다. public String toString() { return String.format("[Novice] %s(HP: %d)", this.name, this.hp); } } this.hp 가 아니라 hp만을 써도 오류없이 출력이 되는데 this를 붙이는 이유가 무엇인가요? #### CODE <a class='btn btn-default' href='/codes/60213'>Link</a> ``` public class Main { public static void main(String[] args) { // 객체 생성 GreatWizard gandalf = new GreatWizard("간달프", 100, 100, 100); // 상태 출력 System.out.println(gandalf.toString()); // 에너지볼트 gandalf.energeVolt(); } } class Novice { // 필드 protected String name; protected int hp; // 생성자 public Novice(String name, int hp) { this.name = name; this.hp = hp; } // toString public String toString() { return String.format("[Novice] %s(HP: %d)", name, hp); } } class Wizard extends Novice { // 필드 protected int mp; // 생성자 public Wizard(String name, int hp, int mp) { super(name, hp); this.mp = mp; } // 에너지볼트 public void energeVolt() { System.out.printf("%s의 에너지볼트!\n", name); } } class GreatWizard extends Wizard { /* 1. 보호막 속성을 필드에 추가하시오. */ protected int shield ; /* 2. 생성자를 완성하시오. */ public GreatWizard(String name, int hp, int mp, int shield) { super(name, hp, mp); this.shield = shield ; } /* 3. toString() 메소드를 오버라이딩 하시오. */ public String toString() { return String.format("[대마법사] %s(HP: %d, MP: %d, SHIELD: %d)", name, hp, mp, shield); } /* 4. 에너지볼트 마법을 오버라이딩 하시오. */ public void energeVolt(){ System.out.printf("%s의 에너지볼트! (대마법사 버프로 데미지 +30 추가)", name); } } ``` #### INPUT ``` ``` #### OUPUT ``` [대마법사] 간달프(HP: 100, MP: 100, SHIELD: 100) 간달프의 에너지볼트! (대마법사 버프로 데미지 +30 추가) ```
## this가 없는 변수는 지역변수, 클래스변수, 인스턴스 변수 중 하나가 되는데 여기에 this를 붙이면 해당 변수가 인스턴스 변수임를 더 명확히 나타낼 수 있습니다. 더 자세한 내용은 "자바 this 인스턴스 변수" 정도로 구글링해보세요