title: Abstract class : Why newInstance() is not giving compilation error but constructor call gives error?
body: <p>Compiler knows that <code>AbstractDemo</code> is an abstract class and Abstract classes can't be instantiated. </p>
  
  <p>But when I call <code>newInstance()</code> method, why it did not give a compile time error?</p>
  
  <pre><code>import java.lang.reflect.Constructor;
  
  public abstract class AbstractDemo{
      public AbstractDemo(){
          System.out.println("Default constructor");
      }
      public static void main(String args[]){
          try{
              /* No compilation error for this statement */
              AbstractDemo demo = AbstractDemo.class.newInstance(); 
  
              Constructor[] ctors = AbstractDemo.class.getDeclaredConstructors();
              for ( int i=0; i &lt; ctors.length; i++){
                  System.out.println(ctors[i]);
                  /* No compilation error for this statement too */
                  AbstractDemo demo1 = (AbstractDemo) ctors[i].newInstance();
              }
              /* Compilation error here */
              // AbstractDemo demo2 = new AbstractDemo(); 
          }catch(Exception err){
              err.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Output when I ran this program: ( I know that error will come since I can't create instance for abstract class. But why it was not given at compile time is surprising me)</p>
  
  <pre><code>D:\Study\Java&gt;java AbstractDemo
  
  java.lang.InstantiationException
          at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
          at java.lang.Class.newInstance(Class.java:374)
          at AbstractDemo.main(AbstractDemo.java:10)
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p><strong><em>Compiler is intelligent to give error for this statement:</em></strong></p>
  
  <pre><code>AbstractDemo demo2 = new AbstractDemo(); 
  </code></pre>
  
  <p><strong><em>But not for this statement</em></strong></p>
  
  <pre><code>AbstractDemo demo = AbstractDemo.class.newInstance(); 
  </code></pre>
  
  <p>Am I missing any key lesson here? </p>