Shouldn’t “static” patterns always be static?

  1. Yes, the whole point of pre-compiling a Pattern is to only do it once.
  2. It really depends on how you’re going to use it, but in general, pre-compiled patterns stored in static fields should be fine. (Unlike Matchers, which aren’t threadsafe and therefore shouldn’t really be stored in fields at all, static or not.)

The only caveat with compiling patterns in static initializers is that if the pattern doesn’t compile and the static initializer throws an exception, the source of the error can be quite annoying to track down. It’s a minor maintainability problem but it might be worth mentioning.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)