Sobes.tech
Junior — Middle

Is it allowed to declare a local variable with the same name as a method parameter?

sobes.tech AI

Answer from AI

In Java, a local variable can have the same name as a method parameter, but this is not recommended because the parameter will be hidden by the local variable within the method. This is called "shadowing". For example:

void example(int value) {
    int value = 10; // Compilation error: duplicate variable name
}

The compiler will give an error if you declare a local variable with the same name as a parameter. However, you can use a parameter with such a name without declaring a new local variable with the same name.