メソッドでは、スコープを作成して、一部の変数へのアクセスを制限できます。
void func() {
{
int num = 3;
}
// num is not accessible here
}
クラスでは、特定のフィールドが特定のメソッドによってのみアクセスできるように、スコープ (または同様の構造) を作成するにはどうすればよいでしょうか?
class MyClass {
private String myHeart = "pure";
void method_friendly {
// should able to access myHeart
}
// ---------- methods under this line should not access myHeart
void method_evil {
// please don't touch myHeart
}
}