Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Boolean.class.newInstance() throw exception?

Tags:

java

I think the sunject's pretty self-explanatory. I use JDK 1.6.0 update 26, and created a new project with just one line to confirm this:

Boolean.class.newInstance();

and it throws the following:

Exception in thread "main" java.lang.InstantiationException: java.lang.Boolean
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)

Does it suppose to fail? If so, why?

like image 795
Max Yankov Avatar asked Nov 28 '22 18:11

Max Yankov


1 Answers

The Boolean class has two constructors, both take one argument. Calling Boolean.class.newInstance() is trying to call a zero-arg constructor that doesn't exist.

like image 105
Rob Harrop Avatar answered Dec 27 '22 10:12

Rob Harrop