Name: mc57594 Date: 02/10/97
Below is a simple program. It takes one argument, but in this
revision the argument is ignored. It shows that if you create
an instance of an AWT subclass by itself, the program does not
exit completely. Ctrl-C must be used to stop it. This seems
to occur when making the new instance causes the message:
'Loading d:\JDK\JAVA\bin\..\lib\awt.properties'.
I came across
this problem while doing some beans stuff. I wish to instantiate
a bean. The Java Beans docs on page 88 say you simply use the
new constructor, which is what I am trying to do.
My real problem may be worse. It is intended that the argument
passed to this program will be a class name. I must convert
this class name dynamically to a class and create an instance of that
bean. In this case, I can not use the new constructor. Is
using newInstance() the recommended way?
-----
Case 1. Simply use the new constructor
----
package TestBeanReader;
import sun.demo.buttons.*;
public class BeanProblem {
public static void main(String args[]) {
BeanProblem beanProblem = new BeanProblem();
beanProblem.start(args[0]);
}
public String start(String beanClassName) {
OurButton ob = null;
ob = new OurButton();
return "ok";
}
}
---------
Case 2- Use the argument to make an instance. Only changing the start() method.
The argument has a ".class" extension.
This will probably be solved in the same way as Case 1.
-------
public String start(String beanClassName) {
Class beanClass = null;
Object bean;
try { beanClass = Class.forName(beanClassName.substring(0,beanClassName.length()-6)); }
catch (Exception ex) {System.out.println("Class not found"); return null;}
if(beanClass == null) {System.out.println("The class was not found"); return null;}
try {bean = beanClass.newInstance();}
catch (Exception ex) {System.out.println("Can't make new instance"); return null;}
return "ok";
}
company - Objectshare , email - ###@###.###
======================================================================