This is because static fields are assigned after the class is loaded (only happens once) in the JVM. The PRIVATE_AREA variable PRIVATE_AREA not be updated when the DOMAIN variable changes.
public class Test { public static String name = "Andrew"; public static String fullName = name + " Barnes"; public static void main(String[] args){ name = "Barry"; System.out.println(name);
I suggest you use the following structure.
public class Test { private static String name = "Andrew"; public static String fullName = name + " Barnes"; public static void setName(String nameArg) { name = nameArg; fullName = nameArg + " Barnes"; } }
Test2.java
public class Test2 { public static void main(String[] args){ System.out.println(Test.fullName);
source share