JAVA/클래스

생성자

별초롱언니 2025. 5. 8. 09:56

생성할때 

1. 인스턴스 변수 초기화

2. 주소반환

 

기본생성자

클래스 이름 () {}

** 기본적으로 주소 return **

package chapter05;

public class Cellphone {
	String model = "Galaxy 8";
	String color;
	int capacity;
	
	Cellphone (String color, int capacity) { // 매개변수가 있는 생성자 
		this.color = color;
		this.capacity = capacity;
	}
}
package chapter05;

public class exam39 {

	public static void main(String[] args) {
		Cellphone myphone = new Cellphone("Silver", 64); // 생성자 호출 
		
		System.out.println(myphone.model);
		System.out.println(myphone.color);
		System.out.println(myphone.capacity);
	}

}

'JAVA > 클래스' 카테고리의 다른 글

오버로딩  (0) 2025.05.08
메서드  (0) 2025.05.07
인스턴스변수와 클래스 변수  (0) 2025.05.07
객체지향언어란?  (0) 2025.05.06