How to acces to this variable in java?

I'm beginner in Java, I'm coming from C#. Look at this code:

public class Ampel {
    public Ampel(boolean r, boolean y, boolean g) {
        boolean red = r,
                yellow = y,
                green = g;
    }
    public void GetStand() {
        System.out.println(red);
        System.out.println(yellow);
        System.out.println(green);
    }
}

I can't acces to "red" or "yellow" and "green" in GetStand(). What should I do?

Jon Skeet
people
quotationmark

You're currently declaring local variables in the constructor. You need to declare instance variables. For example:

public class Ampel {
    private final boolean red;
    private final boolean yellow;
    private final boolean green;

    public Ampel(boolean r, boolean y, boolean g) {
        red = r;
        yellow = y;
        green = g;
    }

    // Name changed to follow Java casing conventions, but it's still odd to have
    // a "get" method which doesn't return anything...
    public void getStand() {
        System.out.println(red);
        System.out.println(yellow);
        System.out.println(green);
    }
}

Note that the equivalent C# code would work exactly the same way. This isn't a difference between Java and C#.

people

See more on this question at Stackoverflow