我想从包2中访问包1中的变量。
来自包1的类文件TestDriver.java
public class TestDriver {
private static TestDriver instance = new TestDriver();
private static int check;
private static int envt_num;
public static String envt,port,cpy_key;
public Connection con;
private ManageDBConnection mdbc;
private static String ENCRYPTION_KEY = "0123456789abcdef";
public void TestDriver(){
check = 20;
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"@"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Constants.driver.get("http://" + user + ":" + password + "@" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
来自包2的类文件Constants.java
public class Constants
{
static Properties prop = new Properties();
// propFileName = "properties/environment.properties";
//TestDriver testdriver = new TestDriver();
static String envt = TestDriver.envt;
static String cpy_key = TestDriver.cpy_key;
//public static final
public static final WebDriver driver = new FirefoxDriver();
public static final String InventorySummaryURL ="http://"+envt+"/test/npHome.do?cpyKey="+cpy_key+"&custId=&grpId=0";
public static final String HomeSummary ="http://"+envt+"/test/npIndex.do?cpyKey="+cpy_key+"&custId=&grpId=0";
public static final String UploadStatus ="http://"+envt+"/test/downloadModuleStatus.do?cpyKey="+cpy_key+"&custId=&grpId=0" ;
public static final String ProfileStatus ="http://"+envt+"/test/myProfileStatus.do?cpyKey="+cpy_key+"&custId=&grpId=0";
}
在Constants.java
中,envt
和cpy_key
返回的值为零。我想要Package 1中的值。
答案 0 :(得分:2)
主要问题是,如果以下变量,您将静态字段与实例字段混淆:
private static final int check;
private static final int envt_num;
private static final String user, password;
public static String envt,port,cpy_key;
整个JVM中的是相同的,不在实例构造函数中修改它们,相反,您可以创建一个静态块来更新它们的值,并且如果final
,您可以将它们标记为public final static String envt,port,cpy_key;
static {
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"@"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
user = prop.getProperty("user");
password = prop.getProperty("password");
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}
public void TestDriver(){
Constants.driver.get("http://" + user + ":" + password + "@" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
}
不应该改变。
Template.hello.onRendered(function() {
console.log('onRendered');
});
关键是将静态字段与实例字段分开,你遇到的问题是静态字段在加载类时没有被初始化,但是在创建实例时。
答案 1 :(得分:1)
变量envt和cpy_key是声明的静态但不应该是。通过将变量声明为静态,您告诉编译器/外部开发人员在使用该变量之前不必实例化此类。
在您的代码中,envt和cpy_key变量仅在TestDriver类的构造函数中初始化。当您从另一个类引用它们而不实例化TestDriver时,您将获得 null 值,该值有时会映射到0。
你能做的是:
示例:
public class TestDriver {
private static TestDriver instance = new TestDriver();
private static int check;
private static int envt_num;
public static String envt,port,cpy_key;
public Connection con;
private ManageDBConnection mdbc;
private static String ENCRYPTION_KEY = "0123456789abcdef";
static {
check = 20;
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = TestDriver.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"@"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Constants.driver.get("http://" + user + ":" + password + "@" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}catch(Exception e){
e.printStackTrace();
}
}
这样可以解决您的初始化问题,但这并不能解决您更大的类设计问题。您应该找到一种更好的(更面向对象的)处理数据的方法。你有两个类依赖于彼此的静态变量。这是一个baaaad计划。我建议你封装需要初始化的变量并将它们保存在TestDriver类中。常量类实际上应该只用于你事先知道的事情(散列键标签,数字常量等)。