I have created a class named Player and a class named Car , which has a method called  isOwner(Player player). It has a constructor that is like this:
public Car(String name, Action<Player> action) {
        this.name=name;
        this.action = action;
}
Now in my main code I want to do this:
Car car1 = new Car("BMW", (input ->{
            if (this.isOwner(input)){
                // code 
            }
           }));
But here this references the class I am writing inside (e.g. Main class); also if I put car1 instead of this, it will give error saying car1 may not be initialized.
I would go for a good old-fashioned unfashionable anonymous inner class.
Car car1 = new Car("BMW") {
    @Override protected action(Player input) {
        if (isOwner(input)) {
            // code 
        }
    }
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With