虽然我正在定义一个类,但我更愿意使用以下格式。类别变量与方法变量
public class ClassName{
private String var1 = null;
private Map<String,String> map1 = null;
public void process(){
try{
var1 = "Variable1";
map1 = new HashMap<String,String>();
// Do Some Stuffs Here with the varaibles.
} catch(Exception e){
e.printStackTrace();
} finally{
var1 = null;
map1 = null;
}
}
}
但我的朋友建议我用下面的方式,
public class ClassName{
public void process(){
String var1 = null;
Map<String,String> map1 = null;
try{
var1 = "Variable1";
map1 = new HashMap<String,String>();
// Do Some Stuffs Here with the varaibles.
} catch(Exception e){
e.printStackTrace();
} finally{
}
}
}
我的问题是,这是更好,为什么?
可能的重复http://stackoverflow.com/questions/5539652/using-a-class-variable-vs-sending-local-variable-to-functions-methods – Bikku
有没有更好的办法。这取决于目的。问问你自己,这些变量是否属于我的班级? – JacksOnF1re
添加到@azurefrog评论,这里有一些很好的理由,为什么私人更好地使用http://programmers.stackexchange.com/q/143736 – stackoverfloweth