Bobby Brahmam
Bobby Brahmam

Reputation: 31

Are static variables allowed inside an anonymous inner class? Some say no, but I can run without errors in Eclipse, but couldn't in online compilers

interface Workables {
    void Work();
}

public class AnonymousTry {
    public static void main(String[] args) {

         Workables w = new Workables() {
            static int x = 10;
            public void Work() {
                System.out.println("Working..."+x);
            }
        };
        
        w.Work();
    }

}

I can run without any errors in Eclipse, but couldn't in online compilers. I am confused about whether the anonymous inner class allows static variables inside it or not.

Upvotes: 1

Views: 52

Answers (0)

Related Questions