Java(4): Constructure Method & Field Variables

이규현·2024년 10월 8일

Constructor Methods

Class

선언:

public class ClassName {
	// code
}

호출:

new ClassName();

Method

선언:

public <type> methodName(<type1> par1, ... , <type> parn) {
	// code
}

호출:

methodName(arg1, ... , arg2);

선언 & 호출 예시

Constructor method

객체 생성 시 한번만 실행.
선언: 클래스 이름과 동일

public class ClassName {
	public ClassName(<type> par1, ... , <type> parn) {
    	//code
    }
}

호출:

new ClassName(arg1, ... , argn);

Field Variables

field variable = 상태 변수

객체

  • 변수에 따라 0, false, null로 초기값이 자동으로 매겨진다.

지역 변수 vs 필드 변수

활용 예시

https://github.com/Leekyoohyun/ProgramMethodologyCode/blob/main/src/week5/resultCode3/GrowingEgg.java

0개의 댓글