Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class with main method doesn't compile

Tags:

java

abstract class Manager {

    static void test() {
        System.out.println(12);
    }

    class Manager1 {
        public static void main(String args[]) {
            System.out.println(Manager.test());
        }
     }
}

It's producing a compile time error. Can an abstract class have a static method with void type?

like image 931
anirban karak Avatar asked Feb 14 '26 15:02

anirban karak


1 Answers

Non-static inner classes cannot have static methods - only top-level and static classes can (as per JLS §8.1.3).

Furthermore:

System.out.println(Manager.test());

Manager.test() is void: you can't print that.

like image 128
arshajii Avatar answered Feb 16 '26 04:02

arshajii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!