[3719] in java-interest
Re: Instanciation by name
daemon@ATHENA.MIT.EDU (Gary Aitken)
Thu Nov 23 03:14:12 1995
Date: Wed, 22 Nov 1995 23:50:41 -0700
From: garya@village.org (Gary Aitken)
To: michel@mmania.com (Michel MEYER), java-interest@java.sun.com
In-Reply-To: <v01510101acd512e4a56d@[194.51.174.243]>
>One can read in the JAVA Specification (4.6 Object Creation -- the new
>Operator) that there is a form of the new operator which allows name
>instanciation, by default calling the default constructor.
>
...
>
>b = new ( "Class"+"A" );
...
>However, I tryed this and I got an error message at compile time:
Here's a different way to accomplish the same thing:
class C {
void f() { System.out.println("C.f"); }
}
public class NewTest {
public static void main( String argv[] ) {
try {
Class cls = Class.forName("C") ;// get meta class for object
try {
C c = (C) cls.newInstance();// this creates the object
c.f();
}
catch (java.lang.IllegalAccessException ex) {
}
catch (java.lang.InstantiationException ex) {
}
}
catch (java.lang.ClassNotFoundException ex) {
}
}
}
Gary Aitken garya@village.org
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com