Java Define Global Constants
There seems to be a lot of confusion around the topic of constants in java.
Java define global constants. This technique can be used if the constants are not really global but especially designed to be used in a specific application for example. To turn an ordinary variable into a constant you have to use the keyword final as a rule we write constants in capital letters to differentiate them from ordinary variables. Syntax final float pi 3 14f.
In java there is no global keyword but we can use a public static variable to referring a global variable. The constants defined in the interface are not merely implementation details they are part of the api intentionally so. For example you can create a global variable in java like.
In addition a constant is cached by the jvm as well as our application so using a constant can improve performance. An application level class needs to implement the interface to be able to see the constant definitions. This concept of constants is relevant to us because we often need to declare constants when we write programs in java.
A brief note on the const keyword. Some people make use of integers or strings to define constants while others make use of enums. If you try.
For example a user of a jbutton may need to call button sethorizontalalignment button center. This is not used with constants in fact it s not used at all in the java language. Global variables are not technically allowed in java.
A constant can make our program more easily read and understood by others. A global variable is one declared at the start of the code and is accessible to all parts of the program. You may have noticed in the reserved words list that there is a keyword called.