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

时间:2021-04-22 21:46:34

I have the following method in a class:

我在课堂上有以下方法:

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())
    {
        ...
    }

    ...
}

Will this pattern get re-compiled every time the method is called? Or does it get cached?

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

Should I declare it as a static variable in my class?

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

Thanks

1 个解决方案

#1


8  

Yes, it is best if you declare it as static, in order to avoid performance penalties due to the pattern recompiling each time.

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

#1


8  

Yes, it is best if you declare it as static, in order to avoid performance penalties due to the pattern recompiling each time.

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