title: Java 9 replace Class.newInstance
body: <p><a href="https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#newInstance--" rel="noreferrer">Class.newInstance</a> was deprecated in Java 9:</p>
  
  <blockquote>
  <pre><code>clazz.newInstance()
  </code></pre>
    
    <p>can be replaced by</p>
  
  <pre><code>clazz.getDeclaredConstructor().newInstance()
  </code></pre>
  </blockquote>
  
  <p>The problem is that <a href="https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-" rel="noreferrer">getDeclaredConstructor</a> returns any constructor without regarding the access level.</p>
  
  <p>If I want to replace all occurrences in my code (on different packages/access level) should I use <a href="https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getConstructor-java.lang.Class...-" rel="noreferrer">getConstructor</a> to get the public constructor? </p>
  
  <blockquote>
    <p>the Constructor object of the public constructor that matches the specified parameterTypes</p>
  </blockquote>
  
  <p>Or <strong>can't</strong> I <em>bulk</em> replace all occurrences because it needs to be per case (if a public constructor exists and/or if I have the right access level for the class)?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>getDeclaredConstructor:</p>
  
  <blockquote>
  <pre><code>   return getConstructor0(parameterTypes, Member.DECLARED);
  </code></pre>
  </blockquote>
  
  <p>getConstructor:</p>
  
  <blockquote>
  <pre><code>   return getConstructor0(parameterTypes, Member.PUBLIC);
  </code></pre>
  </blockquote>