私は Java で名前の検索を学んでおり、C++ から来て、Java でコードのブロックをいくつでもネストできる場合でも、最初のネストされたスコープでのみ名前を非表示にできることに興味を持ちました。
// name hiding-shadowing: local variables hide names in class scope
class C {
int a=11;
{
double a=0.2;
//{
// int a; // error: a is already declared in instance initializer
//}
}
void hide(short a) { // local variable a,hides instance variable
byte s;
for (int s=0;;); // error: s in block scope redeclares a
{
long a=100L; // error: a is already declared in (method) local scope
String s; //error: s is alredy defined in (method) local scope
}
}
}
これは、C++ の観点からは奇妙です。必要なスコープの数をネストでき、変数を好きなように非表示にできるからです。これは Java の通常の動作ですか、それとも何か不足していますか?