dimanche 3 avril 2022

Modify fields with reflection (Java 8) [closed]

Why is it possible to use Reflections in Java 8 to modify only the second example and not the first example? Can anyone help :) .-.

public class Test {

    private String name = "Hans";

}

---------------------------------------------------------

public class Test {

    private String name;

    public Test() {
        name = "Hans";
    }

}

----------------------------------------------
Test test = new Test();

Field field = Test.class.getDeclaredField("name");
field.setAccessible(true);
field.set(test, "Paul");




Aucun commentaire:

Enregistrer un commentaire