'Java Main Method Magic Numbers
So im trying to get rid of two magic numbers that i have in my main method. I tried making them static fields but i just get a different checkstyle error. I'm looking for a way to make my main method check out completely with checkstyle.
These are the checkstyle errors i get:
'2000' is a magic number
'262' is a magic number
These are the checkstyle errors when i make them static fields:
Name 'twothou' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.
Name 'twosixtytwo' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.
P.S. if i try to make the variables non static it wont let me compile. Thanks for the help.
Solution 1:[1]
The "Magic Number" warnings are telling you that you should use a numeric constant instead of a hard-coded number in your code.
The other errors just mean that you should use standard naming practices for your identifiers.
Solution 2:[2]
I believe you need only change your field variable names to all CAPS.
Try TWO_THOU and TWO_SIXTY_TWO.
Solution 3:[3]
You can store the value in final int variable then use it probably then you avoid the checkstyle issue.
int hashcode=hashcode+4; // checkstyle violation since 4 is a magic number
final int value=4; int hashcode=hashcode+value; // no violation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | kronion |
| Solution 3 | Hari Sankar |
