Basically, magic number can be used directly in the code:
public class Foo {
public void setPassword(String password) {
// don't do this
if (password.length() > 7) {
throw new InvalidArgumentException("password");
}
}
}
It will also help you to improve the readability of the code and also it is very easy to maintain. One disadvantage is that, if you use the magic number and the max size changes then, you need to change it to both the code locations.
I hope this will help.
Want to know more about Java? Prefer this tutorial on Learn Java.
Want to become a Java Expert? Join Java Training now!!