new 키워드와 함께 사용 - new Student();new 와 함께 호출 됨private 으로 선언되는 경우도 있음new 키워드와 함께 생성자를 호출할 수 있음public Student(){}Student.java
package ch06; public class Student { public int studentNumber; public String studentName; public int grade; public Student() { } // 기본 생성자 public Student(int studentNumber, String studentName, int grade) { this.studentNumber = studentNumber; this.studentName = studentName; this.grade = grade; } public String showStudentInfo() { return studentName + "학생의 학번은 " + studentNumber + "이고 " + grade + "학년 입니다."; } }
- 클래스의 맴버변수와 생성자의 매개변수의 이름이 같은 경우
this키워드를 붙여줍니다.this키워드는 해당 클래스의 맴버변수를 의미합니다. 즉, 자기 자신을 가리키는 키워드다.StudentTest.java
package ch06; public class StudentTest { public static void main(String[] args) { Student studentLee = new Student(); System.out.println(studentLee.showStudentInfo()); Student studentKim = new Student(12345, "kim", 3); System.out.println(studentKim.showStudentInfo()); } }출력 결과