jeudi 8 juillet 2021

Why is the default constructor of a Subclass not available through reflection?

When running this:

import java.util.Arrays;

public class TestA {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(TestB.class.getConstructors()));
    }
}

class TestB extends TestA {

}

The result will be

[]

When calling the default constructor using reflection, the code will crash with NoMethodFoundException: TestB.<init>(). So somehow the JVM doesn't know that there is a default constructor. BUT when you simple call new TestB(), everything is fine (as it should be).

Adding to that, when I decompile the .class files, both classes have a default constructor.

What's going on under the hood or what am I missing?





Aucun commentaire:

Enregistrer un commentaire