Class.forName() throws ClassNotFoundException

前端 未结 1 445
野的像风
野的像风 2020-12-10 10:51

A Thinking in Java program is as follows:

package typeinfo;
import static util.Print.*;

class Candy {
 static { print(\"Loading Candy\"); }
}

class Gum {
          


        
相关标签:
1条回答
  • 2020-12-10 11:21

    Your classes are in the package typeinfo, so their fully-qualified names are typeinfo.Gum, typeinfo.Candy and typeinfo.Cookie. Class.forName() only accepts fully-qualified names:

    Parameters:

    className - the fully qualified name of the desired class.

    Change your code to:

    try {
      Class.forName("typeinfo.Gum");
    } catch(ClassNotFoundException e) {
      print("Couldn't find Gum");
    }
    
    0 讨论(0)
提交回复
热议问题