Java 我应该将模式对象声明为静态吗
我在一个类中有以下方法:Java 我应该将模式对象声明为静态吗,java,regex,static-variables,Java,Regex,Static Variables,我在一个类中有以下方法: public boolean validTransAmt() { FacesContext facesContext = FacesContext.getCurrentInstance(); Pattern p = Pattern.compile("^([0-9]{0,})(([\\.]?)([0-9]{1,2})([\\.]?))$"); String transAmt = getDetails().getAmount(); Match
public boolean validTransAmt()
{
FacesContext facesContext = FacesContext.getCurrentInstance();
Pattern p = Pattern.compile("^([0-9]{0,})(([\\.]?)([0-9]{1,2})([\\.]?))$");
String transAmt = getDetails().getAmount();
Matcher matcher = p.matcher(transAmt);
if (!matcher.matches())
{
...
}
...
}
每次调用该方法时是否会重新编译此模式?还是缓存了它
我应该在类中将其声明为静态变量吗
谢谢是的,最好将其声明为静态,以避免由于每次重新编译模式而导致的性能损失