We'll discuss a good practice in coding which is using Constants.
A constant in Java is used to map an exact and unchanging value to a unique variable name.
Those constants make the code a bit more robust and more human readable. As those constant values can be used in multiple places, it gives you a single point to change the value of the constant if requires.
The static Keyword
A constant in Java is used to map an exact and unchanging value to a unique variable name.
Those constants make the code a bit more robust and more human readable. As those constant values can be used in multiple places, it gives you a single point to change the value of the constant if requires.
(public/private) static final TYPE NAME = VALUE;
Where
Lets see an exampleTYPE
is the type, NAME
is the name in all caps with underscores for spaces, and VALUE
is the constant value;public final class Consts {
public static final int SECONDS_IN_HOUR = 60 * 60 // sure, this will never change, but it will make the code where you use it a lot more readable
public static final String DEFAULT_USERNAME = "Fernando" // this can be used in several places
public static final double AVOGADROS_NUMBER = 6.02214199e23;
private Consts () {
// a private constructor prevents from instantiating the class
}
}
The static Keyword
The static keyword makes that variable to the class instead of a specific instance (Object).
The final Keyword
When we declare a variable to be final we are telling Java that we will NOT allow the variable’s “pointer” to the value to be changed. In other words the final keyword means that once the value has been assigned, it cannot be re-assigned. So if we tried to put in some code later that tries to change a a final variable we would get a compilation error.
The naming convention
A constant is defined by an UPPER_CASE_LETTERS_WITH_UNDERSCORES_INDICATING_SPACES.
Again, it’s not mandatory to use upper case letters with underscores to indicate spaces, but it’s a convention that programmers use and are familiar with in Java.
private vs public Constants
If a constant is private no other classes can see or use that constant. You may use private constants if those constants are only used in that class. Public constants can be used over multiple classes. It is recommended and practiced to keep the constants in a separate class.
Bad practices
It is bad practice to replace every literal by a constant. For example
The final Keyword
When we declare a variable to be final we are telling Java that we will NOT allow the variable’s “pointer” to the value to be changed. In other words the final keyword means that once the value has been assigned, it cannot be re-assigned. So if we tried to put in some code later that tries to change a a final variable we would get a compilation error.
The naming convention
A constant is defined by an UPPER_CASE_LETTERS_WITH_UNDERSCORES_INDICATING_SPACES.
Again, it’s not mandatory to use upper case letters with underscores to indicate spaces, but it’s a convention that programmers use and are familiar with in Java.
private vs public Constants
If a constant is private no other classes can see or use that constant. You may use private constants if those constants are only used in that class. Public constants can be used over multiple classes. It is recommended and practiced to keep the constants in a separate class.
Bad practices
It is bad practice to replace every literal by a constant. For example
private static final int SMALLEST_POSSIBLE_INCREMENTOR = 1 // don't do this; please!
private static final int FIVE = 5 // oh please please don't do this
SO happy coding folks..