-1

メソッドで宣言されたローカル クラスがあり、そのフィールドはプライベートとして宣言されています。ただし、囲んでいるメソッドの本体から直接アクセスすることはできますが、これはなぜですか?

余談ですが、匿名クラスのすべてのフィールドをプライベートとして宣言しましたが、実際にこれを行う利点はありますか? それらにアクセスできるものはありますか?

編集:コード例

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?
    }
}
4

1 に答える 1