![](/img/trans.png)
[英]How to create a Variable with scope global and but should not be a STATIC
[英]How to Create a Global Variable with a condition
我想創建一個全局變量,但我希望它是基於機器操作系統的兩個值之一。 我想我可以通過編寫以下代碼來做到這一點:
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
但我收到一條錯誤消息,說“無法解析符號‘getProperty’ ”,我想要做的甚至可能嗎?
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.