Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Static method variables

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method for a class that uses "java.util.regex.Pattern" and
"java.util.regex.Matcher" to do regular expressions. I want to only have to compile my regular expressions once, so I have been creating static variables like this: (this is just a boiled down example, not my real code)

The problem I have with this is the actual regular expression is not close to where it is used an so it is not really clear what I am trying to find. In the code above it is not too bad, but in some methods I have 30+ regular expressions and it is a mess. What is a better way to do this to make it more clear what I am trying to match? I wish I could do something like this:


Or if there was native support of regex like in perl that would be much cooler, but I dought that will ever happen.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you want to compile the pattern only once, but you want to have that pattern specified in a method. How about this:

This is an example of "lazy instantiation". If "checkStuff()" is never called, the pattern is never compiled. If it is called, the pattern compilation only happens the first time in.
If this doesn't address your concern, please supply additional information and re-post.
[ January 13, 2004: Message edited by: Wayne L Johnson ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Fletcher:
What is a better way to do this to make it more clear what I am trying to match?


Wayne's suggestion is good. Another one, which I hope you're already following, is to give the static variables better names than "mypattern" -- the name should describe what's being matched. FILENAME, IDENTIFIER, SSN, etc, would all be good names for these variables.
 
Kevin Fletcher
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is definitely better, Thank you.
If I could just get rid of the class wide static variable somehow, it would be perfect. I tried doing this by creating a static variable inside an inner class inside of the method, but I keep getting a compiler complaint that inner classes can't have static declarations. Is there any way around that?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can declare your static outside of any method, but down amongst the methods.

That would put the declaration close to the code. But it sounds really non-conventional and ugly. What do you think?
 
reply
    Bookmark Topic Watch Topic
  • New Topic