Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a java class contain a default constructor?

Tags:

java

oop

I want to check if a Java class contains a default constructor.

Case 1 :

public class ClassWithConstructor {

    //attributes...

    //default constructor
    public ClassWithConstructor(){}

}

Case 2 :

public class ClassWithoutConstructor {

    //attributes...

    // no default constructor

}

In case 1 , I want to print "ClassWithConstructor contains a default constructor."

In case 2, I want to print "ClassWithoutConstructor doesn't contain any default constructor".

like image 409
James Avatar asked Oct 21 '25 16:10

James


1 Answers

You can inspect your class via the Java Reflection API, there is a class called Constructor (see http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/package-frame.html). Not sure, though, whether you can actually distinguish the Java default constructor and a parameterless one you defined on your own.

like image 133
endowzoner Avatar answered Oct 24 '25 07:10

endowzoner