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.
精彩评论