Skip to main content
Asked
Modified 5 months ago
Viewed 135k times
24

I would like to use the following constant:

final String ADD = "Add text";

However, my CheckStyle tool tells me that 'ADD' does not match the pattern '^[a-z][a-zA-Z0-9]*$'.

Could anyone please tell me what is wrong with 'ADD'? Means '^[a-z][a-zA-Z0-9]*$' that every name has to start with a low character? Is there no other possibility?

2
  • 4
    Local variables, class fields and instance fields should be capitalized camelCase. ALL_CAPS is for static finals.
    – ignis
    Commented Oct 30, 2012 at 20:05
  • 1
    ^ means start-of-string, [a-z] means a character between a and z inclusive matched 1 time (= 1 lower case letter), [a-zA-Z0-9]* means 'zero or more characters in [a-zA-Z0-9]', and $ is end-of-string.
    – ignis
    Commented Oct 30, 2012 at 20:12

4 Answers 4

38
^[a-z][a-zA-Z0-9]*$

This regex describes something which starts with lowercase and the remainder is composed of uppercase, lowercase, and numbers. (Examples: aVariable, variable, aNewVariable, variable7, aNewVariable7.)

If you want your field to be constant and static, use:

static final String ADD = "Add text";

Otherwise, use:

final String add = "Add text";
2
  • 1
    First line of the OP's question: i would like to use the following constant. So it's definitely a constant that's wanted. But it's good that you explain the regex. +1 for that. Commented Oct 30, 2012 at 20:10
  • @SimonAndréForsberg True but it may be defined within a method (though I wasn't clear on that). Good point anyway, though. ;)
    – Cat
    Commented Oct 30, 2012 at 21:46
14

If it is a constant you want, it should also be static

static final String ADD = "Add text";

Constants normally use uppercase letters, but since your variable was not static, it was not interpreted as a constant.

1

This Regex indicate the need for camelCase with the first letter being small and then every next word having the first letter in it as capital letter.

1

I just ran into the same problem, turns out it was because it is expected for the Java codebase I was working on to use camel case for all variables as the naming convention. So be sure to check if your variables are named according to the regex pattern ^[a-z]([a-z0-9][a-zA-Z0-9]*)?$. In my case, I got stuck in the Python mode and had my variable named version_regex instead of versionRegex. Once I have made the needed correction the error is no longer thrown.

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

- Stack Overflow">
Skip to main content
Asked
Modified 5 months ago
Viewed 135k times
24

I would like to use the following constant:

final String ADD = "Add text";

However, my CheckStyle tool tells me that 'ADD' does not match the pattern '^[a-z][a-zA-Z0-9]*$'.

Could anyone please tell me what is wrong with 'ADD'? Means '^[a-z][a-zA-Z0-9]*$' that every name has to start with a low character? Is there no other possibility?

2
  • 4
    Local variables, class fields and instance fields should be capitalized camelCase. ALL_CAPS is for static finals.
    – ignis
    Commented Oct 30, 2012 at 20:05
  • 1
    ^ means start-of-string, [a-z] means a character between a and z inclusive matched 1 time (= 1 lower case letter), [a-zA-Z0-9]* means 'zero or more characters in [a-zA-Z0-9]', and $ is end-of-string.
    – ignis
    Commented Oct 30, 2012 at 20:12

4 Answers 4

38
^[a-z][a-zA-Z0-9]*$

This regex describes something which starts with lowercase and the remainder is composed of uppercase, lowercase, and numbers. (Examples: aVariable, variable, aNewVariable, variable7, aNewVariable7.)

If you want your field to be constant and static, use:

static final String ADD = "Add text";

Otherwise, use:

final String add = "Add text";
2
  • 1
    First line of the OP's question: i would like to use the following constant. So it's definitely a constant that's wanted. But it's good that you explain the regex. +1 for that. Commented Oct 30, 2012 at 20:10
  • @SimonAndréForsberg True but it may be defined within a method (though I wasn't clear on that). Good point anyway, though. ;)
    – Cat
    Commented Oct 30, 2012 at 21:46
14

If it is a constant you want, it should also be static

static final String ADD = "Add text";

Constants normally use uppercase letters, but since your variable was not static, it was not interpreted as a constant.

1

This Regex indicate the need for camelCase with the first letter being small and then every next word having the first letter in it as capital letter.

1

I just ran into the same problem, turns out it was because it is expected for the Java codebase I was working on to use camel case for all variables as the naming convention. So be sure to check if your variables are named according to the regex pattern ^[a-z]([a-z0-9][a-zA-Z0-9]*)?$. In my case, I got stuck in the Python mode and had my variable named version_regex instead of versionRegex. Once I have made the needed correction the error is no longer thrown.

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