Search code examples
javaargumentssyntax-errornamed-parameters

Pass named parameter to a method


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

What does this mean, and how can I correct it?


Solution

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

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