Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4039848

exceptions thrown by Class.newInstance() is unclear

XMLWordPrintable

    • beta
    • generic, x86
    • generic, windows_95, windows_nt



      Name: sgC58550 Date: 03/18/97


      According to the Core Reflection API documentation, the
      following should chuck an IllegalAccessException:

      class Z {
       static void doIt (Class c) {
              try {
               c.newInstance(); // create instance of inaccessible class
              }
              catch (Exception e) {
              }
          }
      }

      public class B {
          private class A {
          }

          static Class getPrivateClass () {
           return A.class;
          }

          public static void main (String[] args) {
              Z.doIt(getPrivateClass());
          }
      }

      However this incorrectly raises a NoSuchMethodError.
      ======================================================================


      Name: tb29552 Date: 03/18/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      Class.newInstance won't instantiate an inner class.

      In this code outerFoo is not an inner class and can be instantiated
      via new and Class.newInstance. innerFoo is an inner class that
      can be instantiated with new, but Class.newInstance fails with an
      InstantiationException.

      import java.lang.Class.*;

      class outerFoo { };

      class newInstanceBug {

      public class innerFoo { }

        static void main(String args[]) {
      newInstanceBug nib = new newInstanceBug();
      nib.doIt();
         }
        
        void doIt() {
         try {
         Object o = new outerFoo();
      System.out.println("got outerFoo");

      o = new innerFoo();
      System.out.println("got innerFoo");

      o = outerFoo.class.newInstance();
      System.out.println("got outerFoo again");

      o = innerFoo.class.newInstance();
      System.out.println("got innerFoo again");
         } catch (InstantiationException ie) {
         ie.printStackTrace();
         } catch (IllegalAccessException iae) {
         iae.printStackTrace();
         }
      }


      }



      C:\>\jdk1.2.2\bin\java -cp . newInstanceBug
      got outerFoo
      got innerFoo
      got outerFoo again
      java.lang.InstantiationException: newInstanceBug$innerFoo
              at java.lang.Class.newInstance0(Native Method)
              at java.lang.Class.newInstance(Class.java:241)
              at newInstanceBug.doIt(newInstanceBug.java:25)
              at newInstanceBug.main(newInstanceBug.java:11)
      (Review ID: 100912)
      ======================================================================

            dkramersunw Douglas Kramer (Inactive)
            sgoodsunw Sheri Good (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: