Class<T>.newInstance 到底如何用呢 System.out.println(new Integer(0).getClass().newInstance());//或者这样写都不对Integer.class.newInstance() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 因为 Integer 没有 默认的构造器,无法直接调用newInstance() 对头 Integer貌似有一个构造器new Integer(int i);所以你可以这样写 try { System.out.println(Integer.class.getConstructor(int.class).newInstance(1)); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class Test { public static void main(String[] args) { Class<Integer> c = Integer.class; try { int var = c.getConstructor(int.class).newInstance(111); System.out.println(var); } catch(Exception e) { e.printStackTrace(); } }} Integer没有无参数的public构造器啊。 所以不行咯。 java如何调用exe文件 一道题目 Java本类中方法的调用问题 哪有jrockit下载? Jtree 数据库 出现java.lang.NullPointerException 新手求救,SDK的版本问题 大家帮我看看是不是跟系统有关 求助:java读取数据库问题? 看一下我的问题 ResultSet rs=pstmt.executeQuery();执行总出错,求问 整理一下前面import语句出现的问题,希望大侠们来解惑 Java获取USB错误
对头 Integer貌似有一个构造器new Integer(int i);所以你可以这样写
try {
System.out.println(Integer.class.getConstructor(int.class).newInstance(1));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Test {
public static void main(String[] args) {
Class<Integer> c = Integer.class;
try {
int var = c.getConstructor(int.class).newInstance(111);
System.out.println(var);
} catch(Exception e) {
e.printStackTrace();
}
}
}