一个程序里面有静态变量,我想执行好几遍该程序,每次的变量值都从外面获取,我的静态变量应该写成什么样?
是不是不能用静态的了?
个程序里面有静态变量,我想执行好几遍该程序,每次的变量值都从外面获取,我的静态变量应该写成什么样?
是不是不能用静态的了?
个程序里面有静态变量,我想执行好几遍该程序,每次的变量值都从外面获取,我的静态变量应该写成什么样?
是不是不能用静态的了?
private static String a;
static{
a = xxxx; //可以读取配置文件等等
}
用静态就是负责少变化的数据或者少初始化类,减少内存消耗。例如一般的标识不会改变就用静态。或者类似集合,模型类等哪怕他的一些内部数据经常变动,也可以设置成静态的。就好像一个水果篮就是一个集合,他里面的水果可能经常变化,但水果篮一直在用,减少了消耗,而不是买一次水果就搞一个新的水果篮
你为和要加静态呢,只是单纯想享受静态的优先级调动你的变量吗?你想要你的变量每次都是从外面拿进来,而且要加静态,那么只能是设计你的引用地址不变,实际储存的数据是变化的,但是意思何在呢?你得多说一点你这么设计的需求啊!
楼主,你应该列出你的业务场景,这涉及到程序设计,不然没法给你答案
看下你的代码设计是否有问题呢?通常静态变量设计为私有熟悉,并且提供给外界get/set方法。静态变量是类变量,是全局的。
而你的需求貌似是每执行一次,就从外界获取一次,这种属性应该是跟对象有关,不应该设置为静态变量吧。
我发下主要代码吧,涉及到静态变量的。主要是想把文件路径filename和获取的source的名字改成非静态的
public class ProcessRow {
static Properties source;
static {
try {
source = new Properties();
FileInputStream fis = new FileInputStream(new File(
inputPropertieName()));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
source.load(isr);
fis.close();
} catch (Exception e) {
// throw new ExceptionInInitializerError(e);
}
}
public static String filterAlphabet(String alph) {
alph = alph.replaceAll("[^(A-Z)]", "");
return alph;
}
public String Pname() {
String hai = UseProcess.fileName;
String a = filterAlphabet(hai);
String b = hai.substring(hai.indexOf(a), hai.indexOf(a) + 6);
return b;
}
public static String inputPropertieName() {
/*
* System.out.println("请输入配置文件名称:"); Scanner a = new Scanner(System.in);
* String propertieName = a.next();
*/
// a.close();
ProcessRow a = new ProcessRow();
String propertieName = a.Pname();
return propertieName;
}
// 判断方法1,2
public boolean panduan1(XSSFRow from) {
return from.getCell(8).toString().trim()
.equals(source.getProperty("I列值1"))
&& (from.getCell(2).toString().trim().equals(source
.getProperty("C列值1")));// 1 I列值为“I列值1” 且C列值为“C列值1”
}
public boolean panduan2(XSSFRow from) {
return from.getCell(8).toString().trim()
.equals(source.getProperty("I列值2"))
&& (from.getCell(2).toString().trim().equals(source
.getProperty("C列值2")));// 2 I列值为“I列值2” 且C列值为“C列值2”
}
.........
Output类
public class Output {
static Date d = new Date();
static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
final static String fileName = df.format(d);
static String b = null;
static String c = ProcessRow.source.getProperty("输出路径")+fileName + "_main" + ".xlsx";
public static FileOutputStream init() {
FileOutputStream s = null;
try {
s = new FileOutputStream(c);
} catch (FileNotFoundException e) {
}
return s;
}
public static XSSFWorkbook getWorkbook() throws IOException{
FileInputStream fis = new FileInputStream(c);
XSSFWorkbook wb = new XSSFWorkbook( fis );
return wb;
}
}
UseProcess类
public class UseProcess {
static String fileName=UseProcess.fileName();
static XSSFSheet readsheet;
static {
try {
readsheet = readWorkbook();
} catch (Exception e) {
// TODO: handle exception
}
}
static XSSFWorkbook sc = null;
static XSSFWorkbook sr = null;
public static String fileName() {
System.out.println("请输入文件路径(将文件拖入程序):");
Scanner a = new Scanner(System.in);
String fileName = a.next();
//a.close();
return fileName;
}
ArrayList<String> date1 = new ArrayList<String>();
ArrayList<Double> value1 = new ArrayList<Double>();
ArrayList<String> date2 = new ArrayList<String>();
ArrayList<Double> value2 = new ArrayList<Double>();
int lastnum = getNum(fileName);
int i = Integer.parseInt(ProcessRow.source.getProperty("表单数据从第几行开始")) - 1;
static int j = 1;
public static int getNum(String Filename) {
try {
InputStream myxls;
myxls = new FileInputStream(Filename);
sr = new XSSFWorkbook(myxls);
XSSFSheet sheet = sr.getSheetAt(0);// 第一个工作表
return sheet.getLastRowNum();// 获取第一个工作表的行数
} catch (IOException e) {
return 0;
}
}
主函数
public class TestMain {
public static void test() throws IOException, NumberFormatException, ParseException{
UseProcess.createWorkbook();
UseProcess n = new UseProcess();
for (int i = 1; i <= Integer.parseInt(ProcessRow.source
.getProperty("condition-process的个数")); i++) {
n.UseProcess1(String.valueOf(i));
}
n.UseProcessNull();
System.out.println("输入完毕");
}
public static void main(String[] args) throws NumberFormatException, IOException, ParseException {
TestMain.test();
}
}