How to use method inside anonymous class in java

32 Views Asked by At

how can I use both methods that test() and taste() method outside the anonymous class that is inside the main method?

java code

class Popcorn {
    public void taste() {
        System.out.println("Salty");
    }
}

public class Example1 {

    public static void main(String[] args) {

        Popcorn p2 = new Popcorn() {
            public void taste() {
                System.out.println("hi");
            }

            public void test() {
                System.out.println("hi");
            }
        };

        // here I want to use test() and taste() both.

    }

}
0

There are 0 best solutions below