Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static method create inner class with generic

Tags:

java

generics

It 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() {

        };
    }
}

Though it is compiled successfully, there is error info in main of OuterClass while no error in main of OuterClassTest. enter image description here I wonder why it is inconsistency. I'm using jdk8 and the newest IDEA.

like image 541
Gipple Lake Avatar asked Apr 01 '26 00:04

Gipple Lake


1 Answers

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

like image 187
guenhter Avatar answered Apr 02 '26 12:04

guenhter



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!