I want the value of fileCheck to change when the value of bit is changed
class HelloWorld {
static String bit = "1";
static void changeBit(String profile) {
if(profile.contains("0")) {
bit = "0";
}
}
static String fileCheck = "check" bit "file";
private static void checkFile() {
System.out.println("val " fileCheck);
}
public static void init(){
changeBit("file0");
System.out.println("Value of File from init " fileCheck);
}
public static void main(String[] args) {
HelloWorld obj = new HelloWorld();
obj.init();
obj.checkFile();
}
}
Output:
Value of File from init check1file
val check1file
CodePudding user response:
The two commentators are right:
Since the value for fileCheck is only calculated once you always see the initial value. You have to update the value whenever the value of bit
changes. Here is a possible version:
static void changeBit(String profile) {
if(profile.contains("0")) {
bit = "0";
fileCheck = "check" bit "file";
}
}