我应该将模式对象声明为静态

时间:2011-07-18 10:02:11

标签: 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();
    Matcher matcher = p.matcher(transAmt);

    if (!matcher.matches())
    {
        ...
    }

    ...
}

每次调用该方法时,是否会重新编译此模式?或者它是否被缓存?

我应该在班上将它声明为静态变量吗?

由于

1 个答案:

答案 0 :(得分:8)

是的,最好将其声明为静态,以避免因每次重新编译而导致性能下降。