Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

객체 자신을 가리키는 this 본문

Java/객체지향

객체 자신을 가리키는 this

Seung__ 2021. 8. 10. 00:09

1. this.


  • 인스턴스 자신의 메모리를 가리킴
    public void setName(String name){	
       this.name = name;
     }​
  • 생성자 안에서 또 다른 생성자를 호출할 때 사용.
    - 클래스에 생성자가 여러 개인 경우, this를 이용하여 생성자에서 다른 생성자를 호출할 수 있음.
    - 생성자에서 다른 생성자를 호출하는 경우, 인스턴스 생성이 완전하지 않은 상태이므로,
      this() statement이전에 다른 statement 쓸 수 없음
    public class Person{
    
    	private String name;
    	private int age;
        
        public Person(){
        	this("이름없음", 1);
        }
        
        public Person(String name, int age){
        	this.name = name;
    		this.age = age;
         }
    }​
  • 자신의 참조값을 반환함
Comments