I receive the following error message:

Static method cannot be referenced from a non static context: System.Pattern System.Pattern.compile(String)

Here is my code:

    private static Pattern pattern = Pattern.compile(String str);

public static List<String> getRecord(String record) {
        List<String> patternRecord = new List<String>();
        if(record == null || record.equals('')) {
            return patternRecord;

        Matcher recordMatcher = pattern.matcher(record);
        while(recordMatcher.find()) {
            String entireRecord = recordMatcher.group();
            if(entireRecord != null && !entireRecord.equals('')) {
                List<String> splitRecord = entireRecord.split('[,]');
                for(String rec :splitRecord){
        return patternRecord;

Please advise how to avoid such error?

  • 2
    Please edit your question you include more surrounding context. – Adrian Larson Mar 7 '18 at 15:27
  • 2
    Pattern.compile() takes an argument that you omit here. That should produce a different error; getting the "wrong" error sometimes indicates there are other syntax issues. – David Reed Mar 7 '18 at 15:29

You need to actually pass in a string ('...'). Also I advise against introducing a naming conflict by using a variable name of pattern.

Instead of:

static Pattern pattern = Pattern.compile(String str);


static Pattern myPattern = Pattern.compile('<some expression here>')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.