Search code examples
javaglobal-variables

How to Create a Global Variable with a condition


I want to create a global variable, but I want it to be one of two values, based on the machine's OS. I thought I could do this by writing the following code:

import java.lang.*;
import java.util.Properties;

public class constants {
    public static String driverFile = null;
    public static String storageDirectory = null;
        if (System.getProperty("os.name").startsWith("Windows")){
        storageDirectory = "C:\\Scripts\\Price_Tracker";
        driverFile = "geckodriver-v0.27.0-win64.exe";
    }else{//is linux
        storageDirectory = "/Price_Tracker";
        driverFile = "geckodriver-v0.27.0-linux64";
    }// end if(System.getProperty("os.name").startsWith("Windows")

}//end public class constants

but I get an error saying " Cannot resolve symbol 'getProperty' " , is what I'm trying to do even possible?

Cannot resolve symbol 'getProperty'


Solution

  • public class constants {
        public static String driverFile = null;
        public static String storageDirectory = null;
    
        static {
            if (System.getProperty("os.name").startsWith("Windows")){
                storageDirectory = "C:\\Scripts\\Price_Tracker";
                driverFile = "geckodriver-v0.27.0-win64.exe";
            }else{//is linux
                storageDirectory = "/Price_Tracker";
                driverFile = "geckodriver-v0.27.0-linux64";
            }// end if(System.getProperty("os.name").startsWith("Windows")
        }
    
    }//end public class constants