What is the difference between getDeclaredConstructors and getConstructors in the Class API?

前端 未结 4 1470
遥遥无期
遥遥无期 2021-01-30 19:50

I notice that in the Java Reflection API there are two different methods for invoking constructors: the getDeclaredConstructors/getConstructors method.

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 20:19

    The method 'getDeclaredConstructors' returns only the Constructors that are declared inside the class. The method 'getConstructors' also returns constructors that are not declared inside the class but are inherited from super classes.

    So it depends what you're going to do. There is no right approach, it really depends if you also need super constructors.

提交回复
热议问题