List of usage examples for java.lang Class newInstance
@CallerSensitive @Deprecated(since = "9") public T newInstance() throws InstantiationException, IllegalAccessException
From source file:X.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getInt(x)); // Output: 10 f.setInt(x, 20);/*from ww w . j a v a 2 s . c o m*/ System.out.println(f.getInt(x)); // Output: 20 f = clazz.getField("PI"); System.out.println(f.getDouble(null)); // Output: 3.14 f.setDouble(x, 20); }
From source file:X.java
public static void main(String[] args) { try {/*from w w w . j ava 2s.co m*/ Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance(); Class[] argTypes = { String.class }; Method method = clazz.getMethod("objectMethod", argTypes); System.out.println(method.getDeclaringClass()); } catch (Exception e) { System.err.println(e); } }
From source file:X.java
public static void main(String[] args) { try {//from w w w .j a v a2 s.c o m Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance(); Class[] argTypes = { String.class }; Method method = clazz.getMethod("objectMethod", argTypes); System.out.println(method.getDefaultValue()); } catch (Exception e) { System.err.println(e); } }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getByte(x)); // Output: 10 f.setByte(x, (byte) 20); System.out.println(f.getByte(x)); // Output: 20 }
From source file:X.java
public static void main(String[] args) { try {/*from w ww . j av a2 s. c o m*/ Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance(); Class[] argTypes = { String.class }; Method method = clazz.getMethod("objectMethod", argTypes); Object[] data = { "Hello" }; method.invoke(x, data); // Output: Instance method: Hello method = clazz.getMethod("classMethod", (Class<?>[]) null); method.invoke(null, (Object[]) null); // Output: Class method } catch (Exception e) { System.err.println(e); } }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getShort(x)); f.setShort(x, (short) 9); System.out.println(f.getShort(x)); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getBoolean(x)); f.setBoolean(x, false);/* w w w . j a v a 2s . c o m*/ System.out.println(f.getBoolean(x)); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getFloat(x)); f.setFloat(x, 9.99F);/* ww w. ja v a 2s. c o m*/ System.out.println(f.getFloat(x)); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getLong(x)); f.setLong(x, 9L);// w w w. j a v a2 s.c o m System.out.println(f.getLong(x)); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getDouble(x)); f.setDouble(x, 9.99);// w ww. j av a 2s .c o m System.out.println(f.getDouble(x)); }