Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why the value of fileCheck is not changing according to bit

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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";
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading