ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • this
    Java 2023. 2. 13. 03:11
    학습 목표
    this 3가지 사용방법을 알자

    this란
    1.인스턴스(객체) 자신의 메모리를 가리킨다.
    2.생성자에서 또 다른 생성자를 호출할 때 사용할 수 있다.
    3.자신의 주소 (참조값,주소값)을 반환 시킬 수 있다.

     

     

     

    1.인스턴스(객체) 자신의 메모리를 가리킨다.

     

    main 함수에서 Student 객체를 하나 생성한후 학번, 이름을 Setter를 통해 값을 입력했다고 가정한다.

    package ch08;
    
    public class StudentTest {
    			public static void main(String[] args) {
    				Student studentPark = new Student();
    				
    				studentPark.setStuentID(10);
    				studentPark.setStudentName("피망");
    					
    			}
    }
    학생의 정보를 저장하는 Student 클래스에서 멤버 변수를 선언한 이후 Setter를 설정할 때 this.변수명 = 매개 변수 명으로 지정한다. 이때 this 키워드는 자기 자신의 메모리를 가르키기 때문에 멤버변수의 studentID랑 동일한 의미가 된다.

     

    package ch08;
    
    public class Student {
    	
    			private int studentID;
    			private String studentName;
    			
    			public void setStuentID(int studentID) {
    				this.studentID = studentID;
    			}
    			public void setStudentName(String studentName) {
    				this.studentName = studentName;
    			}
    }

     

     

    this를 사용하지 않으면 매개변수 이름과 대입하는 이름이 똑같기 때문에 구분할 수 없어서 에러가 발생한다.

    매개변수의 이름을 멤버 변수와 다른 이름으로 설정한다면 this 키워드를 쓰지 않아도 된다. 하지만 가독성을 위해 매개 변수 명과 멤버 변수를 동일하게 쓰는것을 권장하고 있다.

     

     

     

    2.생성자에서 다른 생성자를 호출할 경우 사용한다.

     

     

    package ch08;
    
    public class Student {
    	
    			private int studentID;
    			private String studentName;
    			public Student() {
    					this(150,"피망");
    			}
    			public Student(int studentID, String studentName) {
    				this.studentID = studentID;
    				this.studentName = studentName;
    			}
    			public void showInfo() {
    				System.out.println("학번 : "+ studentID);
    				System.out.println("이름 : " + studentName);
    			}
    }
    package ch08;
    
    public class StudentTest {
    			public static void main(String[] args) {
    				
    				Student studentPark = new Student();
    				studentPark.showInfo();
    					
    			}
    }
    출력값
    학번 : 150
    이름 : 피망

    메인 함수에서 객체를 생성하고 값을 대입하지 않고 출력할 경우 자동으로 생성자에서 대입 된 값이 출력되는것을 확인할 수 있다.

     

    package ch08;
    
    public class StudentTest {
    			public static void main(String[] args) {
    				
    				Student studentPark = new Student();
    				studentPark.showInfo();
    				
    				Student studentLee = new Student(200,"Lee");
    				studentLee.showInfo();
    					
    			}
    }
    출력값
    학번 : 150
    이름 : 피망
    학번 : 200
    이름 : Lee

    생성자를 호출할 때 값을 대입하면 대입된 값으로 출력된것을 확인할 수 있다.

     

    매개 변수가 없는 생성자에서 this를 사용하여 다른 생성자를 호출할 경우 해당 구문이 첫번째 구문이 되어야한다.

     

     

     

    3.인스턴스 자신의 주소를 반환할 때 사용한다.

     

    Student  클래스에서 반환 타입을 Student로 설정한 getDelf 메서드에서 반환 시 this를 반환한다.

     

    package ch08;
    
    public class Student {
    			private int studentID;
    			private String studentName;
    			
    			public Student(int studentID, String studentName) {
    					this.studentID = studentID;
    					this.studentName = studentName;
    			}
    			public Student getSelf() {
    				return this;
    			}
    }
    package ch08;
    
    public class StudentTest {
    			public static void main(String[] args) {
    				
    				Student studentPark = new Student(200,"Park");
    				System.out.println(studentPark);
    				
    			}
    }
    메인 함수에서 Student 객체를 생성하고 해당 객체 정보 출력 및 test 객체를 하나 더 만들어서 해당 객체에 이전에 생성한 객체의 this 반환 값을 대입한다.

    출력할경우 ch08.Student@49e4cb85 주소값이 나오는것을 확인할 수 있다.
    this가 가르치는 위치와 생성된 객체가 가르치는 위치가 같은것을 확인할 수 있다.

     

     

     

    'Java' 카테고리의 다른 글

    배열  (2) 2023.02.18
    static  (0) 2023.02.13
    접근 제어 지시자  (0) 2023.02.13
    객체와 객체간에 상호작용  (0) 2023.02.12
    생성자  (0) 2023.02.12
Designed by Tistory.