The final keyword in the class Steady

Is there a difference in performance and / or any other benefits that we can get when using the final keyword with a constant utility class. [This class comprises only static finite fields and private constructor to avoid the creation of object]

public class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; public static final String VALIDPHONENUMBER = "\\d{10}"; ... ... } 

Only diffrence is a class that is final

  public final class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; public static final String VALIDPHONENUMBER = "\\d{10}"; ... ... } A-Za-z0-9 \\ s.] +";  public final class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; public static final String VALIDPHONENUMBER = "\\d{10}"; ... ... } d {  public final class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; public static final String VALIDPHONENUMBER = "\\d{10}"; ... ... } 

I like to know whether there is any advantage in using final and what is the right way to define class constants.

+4
source share
5 answers

There is no benefit. It does not change anything in relation to your attributes static final .

When a class becomes final, the compiler can use this to override methods (static methods can not be overridden, in the best case, they hide them in inherited classes).

Since the class is final, the compiler knows that none of its methods can be overridden. Thus, it can calculate cases where the polymorphism code does not need to be generated (i.e., the code that finds the correct "version" of the overriding method according to the instance of the object at runtime). Therefore, optimization is possible.

If you want to make the class truly unique, you can use something like this:

 public enum ActionConstants { INSTANCE; public static final int cte1 = 33; public static final int cte2 = 34; } 

And if you're not at all interested in an instance of a class, just put all your constants in the interface.

+5
source

If you are looking for improved performance, you are better off to compile your templates, such as

 public static final Pattern VALIDFIRSTLASTNAME = Pattern.compile("[A-Za-z0-9.\\s]+"); public static final Pattern VALIDPHONENUMBER = Pattern.compile("\\d{10}"); "[A-Za-z0-9 \\ s.] +"); public static final Pattern VALIDFIRSTLASTNAME = Pattern.compile("[A-Za-z0-9.\\s]+"); public static final Pattern VALIDPHONENUMBER = Pattern.compile("\\d{10}"); 

Using the end or not, is very small compared to the cost of the use of regular expressions.

+3
source

You should avoid the use of "classes for constants." This means bad ddesign. Put constants in classes that work with them. Avoid using public constants. This should be the exception, not the usual practice.

+1
source

There is no real benefit, but it lives up to your expectation that nothing extends your class. This can make it easier in the long run, for example, find the code for all applications of constants, as they are guaranteed to be XXX.abc, not YYY.abc, where YYY is expanding XXX.

0
source

Using a class for constants is unusual. In most cases, an interface is used. Access to it will ActionConstants.VALIDFIRSTLASTNAME :

 public interface ActionConstants { static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; static final String VALIDPHONENUMBER = "\\d{10}"; ... } -Za-z0-9 \\ s.] +"; public interface ActionConstants { static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; static final String VALIDPHONENUMBER = "\\d{10}"; ... } { public interface ActionConstants { static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; static final String VALIDPHONENUMBER = "\\d{10}"; ... } 

Starting with Java 5, you can also use the transfer. An enumeration can have members or advanced functionality.

The second example uses a simple element (here with the general approach, if you have different types of permanent, or you can also use the element String ):

 public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Object value; private ActionConstants(Object value) { this.value= value; } @SuppressWarnings("unchecked") public <T> T getValue() { return (T)value; } } String value = ActionConstants.FIRSTLASTNAME.getValue(); z0-9. \\ s] +"), public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Object value; private ActionConstants(Object value) { this.value= value; } @SuppressWarnings("unchecked") public <T> T getValue() { return (T)value; } } String value = ActionConstants.FIRSTLASTNAME.getValue(); { public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Object value; private ActionConstants(Object value) { this.value= value; } @SuppressWarnings("unchecked") public <T> T getValue() { return (T)value; } } String value = ActionConstants.FIRSTLASTNAME.getValue(); 

The last example uses extended functionality when all the constants are of the same type. You can use it as ActionConstants.PHONENUMBER.isValid("0800123456") :

 public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Pattern pattern; private ActionConstants(String pattern) { this.pattern = Pattern.compile(pattern); } public void isValid(String value) { return pattern.matcher(value).matches(); } } z0-9. \\ s] +"), public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Pattern pattern; private ActionConstants(String pattern) { this.pattern = Pattern.compile(pattern); } public void isValid(String value) { return pattern.matcher(value).matches(); } } ; public enum ActionConstants { FIRSTLASTNAME("[A-Za-z0-9.\\s]+"), PHONENUMBER("\\d{10}"); private final Pattern pattern; private ActionConstants(String pattern) { this.pattern = Pattern.compile(pattern); } public void isValid(String value) { return pattern.matcher(value).matches(); } } 

Both versions allow static import.

0
source

All Articles