Pass named parameter to a method

Michael :

Code:

class AllTheColorsOfTheRainbow {
    private int hue = 0;

    int anIntegerRepresentingColors;    

    void changeTheHueOfTheColor(int newHue) {
        this.hue = newHue;
    }

    public int getHue(){
        return this.hue;
    }
}

public class Ex11 {
    public static void main(String [] args){
        AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
        a.changeTheHueOfTheColor(newHue = 1);
        System.out.println(a.getHue());
    }
}

Stack trace:

 javac Ex11.java 
Ex11.java:18: error: cannot find symbol
        a.changeTheHueOfTheColor(newHue = 1);
                                 ^
  symbol:   variable newHue
  location: class Ex11
1 error

Could you help me understand what does it mean and how to correct it?

Mureinik :

Java does not have named arguments, just positional arguments. You need to pass it without the parameter's name:

a.changeTheHueOfTheColor(1);
// Here -----------------^

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403490&siteId=1