| preferred AP College board partner for AP classes
Login
 
Register
Static Variable Shadowing
< Prev
Next >

What is the mistake in the update method of the Configuration class?

public class Configuration {
    public static int value = 5;
    
    public void update(int newVal) {
        int value = newVal; // Error: A local variable 'value' is declared, shadowing the static variable.
    }
    
    public int getValue() {
        return value;
    }
}
A

It fails to pass the new value by reference, so the update does not persist.

B

It does not include an else statement to handle alternate cases.

C

The update method should be declared static to modify a static variable.

D

It declares a new local variable that shadows the static field, so the static value is never updated.

Check Answer
APFIVE © 2020.
Email: [email protected]|Privacy Policy