메소드에 선언 된 로컬 클래스가 있습니다. 필드는 private로 선언됩니다. 그러나, 나는 여전히 그들을 둘러싼 방법의 몸체에서 직접 접근 할 수있다. 왜 이럴까?로컬 클래스의 액세스 제어 수정 자 - 작동하지 않는 이유는 무엇입니까?
부수적으로 익명 클래스의 모든 필드를 비공개로 선언했지만 실제로이 작업에 어떤 이점이 있습니까? 아무것도 액세스 할 수 있습니까?
편집 :이 중첩 된 클래스와 비슷한 방식으로 액세스 할 수 있기 때문에 코드 예제
public void myMethod() {
class myException extends SomeOtherException{
private boolean Bool;
public Boolean getBool() { return this.Bool; }
public myException() { //constructor stuff }
}
try {
Thing.setHandler(new HandlingClass() {
private String myString; //What is the point in making these private?
... other methods in anonymous class ...
}
... more code ...
} catch (myException e) {
... e.Bool //Can be accessed. Why?
}
}
코드를 추가하십시오. – Fildor
'myString' ** private는 목적이 없다 **; 반사 동작은 약간 다릅니다 (SecurityManager가 설치된 경우). 'new HandlingClass() {...} .myString' 작업을 수행 할 수 있습니다. –