Is there any naming convention for java constant variable?
Normally we use variables with names containing uppercase letters and underscores(_
).
For example:
public final class DeclareConstant { public static final String CONSTANT_STRING = "some constant"; public static final int CONSTANT_INTEGER = 5; }
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)
Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.
To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.
If it's private or protected then it should be all lowercase. If it's public or package then it should be all uppercase.
Yes. That is it. It is often used for enum
as well.
The only common exception is for logging where you might see
private static final Logger log = Logger.getLogger(getClass().getName());
but I prefer LOG
I often write this as UPPER_CASE, but I also write TitleCase for classes and camelCase for variables and methods.
That is right. According to Sun:
Scroll to the bottom see constants
Constants
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With