开发者

Should I declare pattern object as static

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜