A static method creates an inner class with a common

This is a demo about inner class and generic type.

    class OuterClass<T> {

    public OuterClass() {
    }

    public static void main(String[] args) {
        new OuterClass<String>().new InnerAbstractClass() {
        };
    }

    public class InnerAbstractClass {
        T t;
        public void a() {

        }

    }
}
class OuterClassTest {
    public static void main(String[] args) {
        System.out.println(1);
        new OuterClass<String>().new InnerAbstractClass() {

        };
    }
}

      

Even though it compiled successfully, there is error information in the main OuterClass, and there is no error in the main OuterClassTest. enter image description here I wonder why this is inconsistent. I am using jdk8 and the latest IDEA.

+3


source to share


3 answers


The code works. It even compiles (also in IntelliJ). The IDE just shows an error. Try your inner main class and it will work. Perhaps you could open an issue for JetBrains at https://youtrack.jetbrains.com/oauth?state=%2Fissues%2FIDEA



+1


source


If you don't like any warning like this, you can try:



public static void main(String[] args) {
    OuterClass<String> outerClass = new OuterClass<String>();
    outerClass.getInstance();
}

public InnerAbstractClass getInstance() {
    return new InnerAbstractClass();
}

      

0


source


Since InnerAbstractionClass doesn't use any non-static OuterClass members, you can simply make InnerAbstractionClass static to get rid of this error. Postscript - This error has nothing to do with generics.

0


source







All Articles