X.java Source code

Java tutorial

Introduction

Here is the source code for X.java

Source

import java.lang.reflect.Field;

class X {
    public int i = 10;
    public static final double PI = 3.14;
}

public class Main {
    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);
        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);
    }
}