mercredi 21 avril 2021

Java Reflection: Why getConstructors() for class A returns an empty array? [duplicate]

D.java

   package com.model; 
   public class D {
     public int z;
    }

B.java

package com;
import com.model.D;

class A {
 public int x;
}

class C {
 public int y;
 public C() {}
}
        
public class B {
    public static void main(String args[] ) throws Exception {
        
        Class clssA = A.class;
        Constructor[] constructorA = clssA.getConstructors();   // []
        
        Class clssC = C.class;
        Constructor[] constructorB = clssC.getConstructors();  // [public com.C()]

        Class clssD = D.class;
        Constructor[] constructorD = clssD.getConstructors();  // [public com.model.D()]
        }
}

As per my understanding, the Java compiler provides a default (public no-arg) constructor for classes without any constructor, then why the output of getConstrucors() call for class A and class C are different?

Also, class A and class D have the same class structure with the only difference of package name but the getConstructors() for class D returns a non-empty array?





Aucun commentaire:

Enregistrer un commentaire