1

For constant primitive types and data-structure like objects (Strings, Object versions of primitives) the convention seems to be full caps with underscores separating words. Does this convention carry through to things such as thread pools?

Example:

public static final int MAX_SPEED = 500;
public static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool();
user2248702
  • 2,741
  • 7
  • 41
  • 69

2 Answers2

2

In Java constant variables are declared using “static final” modifiers. And such variables must contain only UpperCase charachters and multiple words must be seperated using ‘_’.

1   static final char END_OF_FILE = 'e';
2   myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Conventions was created just to improve readability of code. So its your choice to use them or leave them. But if you do use them, your code will look professional. Java Compiler does expect you to use these conventions. But there are some languages where, the way you name your variables, indicates to the compiler what type of variable it is. For instance, in Ruby, for declaring a constant variable you have to use only Uppercase for entire name of the variable. Ruby compiler identifies constant variables in that way only!

for more details

Java naming convention for static final variables

Community
  • 1
  • 1
Engineer
  • 1,436
  • 3
  • 18
  • 33
1

I dont think that there is any such convention or rule for this. But yes as a good practice people follow what you are doing that names are in full caps and for seperation of words using underscore.

Does this convention carry through to things such as thread pools?

Yes you can follow this.

From JLS:

6.8.5 Constant Names

The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech. Examples of names for constants include MIN_VALUE, MAX_VALUE, MIN_RADIX, and MAX_RADIX of the class Character.

I will say it is all a matter of preference.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331