Java类中的静态变量在程序运行期间,其内存空间对所有该类的对象实例而言是共享的,有些时候可以认为是全局变量。因此在某些时候为了节省系统内存开销、共享资源,可以将类中的一些变量声明为静态变量,通过下面的例子,你可以发现合理应用静态变量带来的好处:
/**
* @author Administrator
*/
public enum FaxTemplate {
酒店通知单, 机票催票, 度假通知;
Map<String, Position> variablePositions = new HashMap<String, Position>();
Map<String, CollectionPosition> collectionPositions = new HashMap<String, CollectionPosition>();
String templatePath;
String resultPath;
String name;
String file;
public FaxTemplate setCollection(String name, String rowRange) {
collectionPositions.put(name, new CollectionPosition(rowRange));
return this;
}
public FaxTemplate setCollectionColumn(String collectionName,
String cellName, String strVal) {
collectionPositions.get(collectionName).set(cellName,
Integer.parseInt(strVal));
return this;
}
public static class CollectionPosition {
public RowRange rowRange;
public Map<String, Integer> cells = new HashMap<String, Integer>();
public CollectionPosition(String rowRange) {
this.rowRange = new RowRange(rowRange);
}
public CollectionPosition set(String cellName, Integer val) {
cells.put(cellName, val);
return this;
}
public Integer get(String cellName) {
return cells.get(cellName);
}
}
public static class RowRange {
public int start;
public int end;
public RowRange(String strVal) {
String[] strs = strVal.split(",");
this.start = Integer.parseInt(strs[0]);
this.end = Integer.parseInt(strs[1]);
}
}
public static class Position {
public int row;
public int col;
public Position(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Position other = (Position) obj;
if (this.row != other.row) {
return false;
}
if (this.col != other.col) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
}
}