티스토리 뷰

class Caller{
	private void init() {
		System.out.println("Initialized");
	}
	private void start() {
		init();
		System.out.println("Started");
	}
}
public class Class50 {
	public static void main(String[] args) {
		Caller c = new Caller();
		c.start();
		c.init();
	}
}

What is the result?

A. An exception is thrown at runtime.

B. Initialized

Started

Initialized

C. Initialized

Started

D. Compilation fails.

 

정답: D

결과:

The method start() from the type Caller is not visible

The method init() from the type Caller is not visible

해설:

결과에서 보다 시피

Caller에 있는 start() 메소드가 보이지 않는다.

Caller에 있는 init() 메소드가 보이지 않는다.

즉, private로 선언되어 있으니 당연히 보이지 않는거 겠죠. private은 외부 클래스에서 접근할 수 없습니다.

내부(본인) 클래스에서만 접근 가능 합니다. start() 메소드 안에 init() 메소드 호출한것처럼요.

예를들어 c.aa() 처럼 아예 없는 메소드 같으면 The method aa() is undefined for the type Caller

즉, Caller 클래스에 정의되지 않았다고 컴파일 에러가 뜰텐데 private으로 접근하려고 했을때는 undefined가 아니고 not visible 이라고 표현하네요. 아마 2개를 구분하려고 메시지를 표시해주는듯 합니다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   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
글 보관함