Require a default constructor in java?

Posted by jdc0589 on Stack Overflow See other posts from Stack Overflow or by jdc0589
Published on 2009-11-18T22:05:15Z Indexed on 2010/05/08 13:58 UTC
Read the original article Hit count: 249

Filed under:
|
|

Is there any way to require that a class have a default (no parameter) constructor, aside from using a reflection check like the following? (the following would work, but it's hacky and reflection is slow)

 boolean valid = false;
 for(Constructor<?> c : TParse.class.getConstructors())
 {
   if(c.getParameterTypes().length == 0) {
      valid = true;
      break; 
   }
 }
 if(!valid)
    throw new MissingDefaultConstructorException(...);

© Stack Overflow or respective owner

Related posts about java

Related posts about constructor