This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.reflect; | |
public class SemiMutable { | |
private final String name = "MAKAN"; | |
public String getName(){return name; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.reflect; | |
import java.lang.reflect.Field; | |
public class TestMutable { | |
public static void main(String[] args) throws Exception{ | |
Field f = SemiMutable.class.getDeclaredField("name"); | |
SemiMutable mutable = new SemiMutable(); | |
try{ | |
String ss = (String) f.get(mutable); | |
System.out.println(ss); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
f.setAccessible(true); // jadi bisa diakses | |
SemiMutable mutable1 = new SemiMutable(); | |
f.set(mutable1, "GIGI"); | |
String s = (String) f.get(mutable1); | |
System.out.println(s); | |
String ss = (String) f.get(mutable); | |
System.out.println(ss); | |
} | |
} |