Java

Java Constant and Constant Enumerations (Enums)

Java Constant

java constant is a variable whose value can’t change whenever it has been allocated. Java doesn’t have underlying help for constants.

A java constant can make our program all the more effectively read and comprehended by others. Likewise, a constant is cached by the JVM just as our application, so using a java constant can improve execution.

To define a variable as a java constant, we simply need to add the keyword “final” before the variable declaration.

Java does not directly define constants. Instead, to prefix the keyword final to the declaration of variables:

The above statement declares the double variable “pi” as a constant with a value of 3.1415927. We can’t change the value of “pi” anytime in the program. Afterward in the event that we attempt to do that by using an assertion like “pi=2.25”, Java will throw errors at compile time itself. It isn’t required that we need to assign values of constants during initialization itself.

It causes the variable not to be initialized after the initial initialization more. Try it anyway, javac will deliver Compile an error message. If final is used for an object variable, the object reference can be no longer be changed. But watch out:The data of the object can be modified! In this respect, a final Object not like a java constant.

Note:

In most classes, final variables are also declared as static. Several objects share the constants. final can also class declaration, but has a Other meaning: Final classes cannot be extended by inheritance.



Static Modifier:

This permits a variable to be utilized without first making an instance of the class; a static class member is related with the class itself, instead of an object. All class instances share a similar duplicate of the variable.

This implies that another application or main() can undoubtedly utilize it.

For example, class demo contains a static variable var:

Since this variable is static, it tends to be utilized somewhere else without explicitly making a demo object:


final Modifier:

The final modifier implies that the variable’s worth can’t change. When the worth is doled out, it can’t be reassigned.

Primitive data types (i.e., int, short, long, byte, char, float, double, boolean) can be made changeless/unchangeable utilizing the final modifier.

Together, these modifiers make a constant variable.

It is a good job to declare the java constant in capital word from this it is easy to understand that it is constant variable as you can see I declared in capital word.


Java Constant Enumerations (Enums)

Use the enum keyword to define a list of java constants. The following listing demonstrates both the definition and the Application of such a java constant enumeration:

Example: how to use Java constant enumeration:

enum has several advantages over the definition of some final variables:

  • You don’t have to worry about what values you have in your constants. The Java compiler generates automatically unique values.
  • Assignments and comparisons are subject to type control. take, you define two enumerations: one for colors (Color) and one for shapes (Geometry – round, triangular, square). The Java Compiler ensures that you do not incorrectly set a variable of type ColorCompare to a constant from the Geometry enumeration.

Note:

Behind the scenes, enum creates a new type similar to class. In this respect, enum provides unequal possibilities for defining enumerations as a simple enumeration of (internally numbered) java Constant.



Related Article:

Input and Output Data in java With Examples

Variables in Java Context: Validity level, Object Variables, Wrapper Classes, Class variables

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button