Static initializer on a inner class?

How to allow static initializer on a inner class like this:

public class MyClass {

    public class InnerClass {
        static {
            // do something
        }
        public bar(){
            // do something
        }
    }

    // method stuff
    public void foo() {
        // do something
    }

} 

Answer

From JLS 8.1.3 Inner Classes and Enclosing Instances:

Inner classes may not declare static initializers (§8.7) or member interfaces.

What you may want is a nested class:

public static class InnerClass { // note "static"

Leave a Reply

Your email address will not be published. Required fields are marked *