集册 Java实例教程 用Class.forName()加载类

用Class.forName()加载类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

459
用Class.forName()加载类

class Cls {

    private Cls() {

    }
    /**
    N o  w  J a v a . c o m - 时  代  Java
    **/

}


public class ClassTrouble {

    public static void main(String... args) {

        try {

            Class<?> c = Class.forName("Cls");

            c.newInstance(); // InstantiationException


            // production code should handle these exceptions more gracefully

        } catch (InstantiationException x) {

            x.printStackTrace();

        } catch (IllegalAccessException x) {
        /*来自 
         N o w J a v a . c o m - 时  代  Java*/

            x.printStackTrace();

        } catch (ClassNotFoundException x) {

            x.printStackTrace();

        }

    }

}


展开阅读全文