Example usage for java.lang Class getField

List of usage examples for java.lang Class getField

Introduction

In this page you can find the example usage for java.lang Class getField.

Prototype

@CallerSensitive
public Field getField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.awt.Point.class;

    Field field = cls.getField("x");
    System.out.println(field);/*w ww.j a  va 2s .  c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    MyClass c = new MyClass();
    Class cls = c.getClass();

    Field sField = cls.getField("string1");
    System.out.println("Public field found: " + sField.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class cls = java.awt.Point.class;

    Field field = cls.getField("x");

    // Get value/* w  w w.  j a  va  2  s.co m*/
    //    field.getInt(object);

    // Set value
    //  field.setInt(object, 123);

    // Get value of a static field
    field.getInt(null);

    // Set value of a static field
    field.setInt(null, 123);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Bean demo = new Bean();
    Class clazz = demo.getClass();

    Field field = clazz.getField("id");
    field.set(demo, new Long(10));
    Object value = field.get(demo);
    System.out.println("Value = " + value);

    field = clazz.getField("now");
    field.set(null, new Date());
    value = field.get(null);/*from www  .j  a va2s.c o m*/
    System.out.println("Value = " + value);
}

From source file:Main.java

public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();
    try {//from w  ww  .jav  a2 s  .  com
        Field heightField = c.getField("height");
        heightField.setInt(r, 1000);
        Integer heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();
    try {/*from ww w. j a va  2  s .c  o  m*/
        Field heightField = c.getField("height");
        Integer heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (NoSuchFieldException e) {
        System.out.println(e);
    } catch (SecurityException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    }
}

From source file:X.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("X");
    X x = (X) clazz.newInstance();//ww w. j  av a  2  s  .  c o m
    Field f = clazz.getField("i");
    System.out.println(f.getInt(x)); // Output: 10
    f.setInt(x, 20);
    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:FieldSpy.java

public static void main(String... args) {
    try {// w w  w .  java 2 s. c  o m
        Class<?> c = Class.forName("FieldSpy");
        Field f = c.getField("name");
        System.out.format("Type: %s%n", f.getType());
        System.out.format("GenericType: %s%n", f.getGenericType());

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

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: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));

}