// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.lang.reflect.Field;

public class ReflectionFun {
    public static void main(String[] args) throws Exception {
        SomeBase oa = new A(1);
        System.out.println(oa);
        oa.reset();
        System.out.println(oa);
        SomeBase ob = new B(2, 3);
        System.out.println(ob);
        ob.reset();
        System.out.println(ob);
    }
}

class SomeBase {
    public void reset() throws IllegalArgumentException, IllegalAccessException {
        for(Field f : getClass().getDeclaredFields()) {
            f.setAccessible(true);
            f.setInt(this, 0);
        }
    }
}

class A extends SomeBase {
    private int a;
    public A(int a) {
        this.a = a;
    }
    @Override
    public String toString() {
        return String.format("A:a=%d", a);
    }
}

class B extends SomeBase {
    private int b1;
    private int b2;
    public B(int b1, int b2) {
        this.b1 = b1;
        this.b2 = b2;
    }
    @Override
    public String toString() {
        return String.format("B:b1=%d,b2=%d", b1, b2);
    }
}
