How to define java constant?

In this article, we will learn about how we can define java constant.

Java constant refers to a fixed value that doesn’t change during the execution of a program. The value of constants appears right in a program. It is also known as Literals. We use the constants to create values that assign to variables. Constants can make our program easy to read and understood by others.

Point to remember

The name of the identifier should be in capital letters that we want to declare as constant. For example,

PRICE=12.

  • As we use the private access-specifier before the constants name. At that point, we can’t change the value of the constant of that particular class.
  • And if we use the public access-specifier before the constants name. In the program, the value of the constant can be change.

How to define constant

Java does not directly support the constant. To define a variable as a constant, We use the “Static” and “Final” Keywords before declaring a variable.

Static and final modifiers.

  • The main goal of using the static modifier is to manage the memory.
  • It also allows the variable to be available without loading any instance of the class. In which we define the variable.
  • The final modifier represents that we can not change the value of the variable. It also makes the primitive data type immutable that unable to change.

Syntax to define constant in Java as follows:

static final datatype identifier_name=value;

for example

static final float rate=2.15f; 

In the above example, we use the variable ‘rate’ as a constant. The value of a variable is 2.15f which is a float data type. We cannot change the value of a variable at any point in the program. If we try to change the value java compiler will throw an error.

Where the static and final are the non-access modifiers. Float is a data type of a variable. The static modifier causes the variable to be available. Without an instance of its defining class start to load. Also, the final modifier makes the variable fixed.

Here is a reason for using both static and final keywords to define the constant.

So if we only use static keywords. Then all the objects of the class can access the variable and the value can change which we don’t want. To solve this issue we use the final modifier keyword.

If we use the final keyword. Then many instances of the same constant value will be created. For the very different object which is not useful.

When we use static and final keywords together. The variable remains static and initialized once. Thus, to declare a variable as constant, we use both static and final keywords. They share a common memory location for all objects of its containing class.

Constant declared as private:

import java.util.Scanner;  
public class ConstantExample1   
{  
//declaring constant   
private static final double PRICE=234.90;  
public static void main(String[] args)  
{  
int unit;  
double total_bill;  
System.out.print("Enter the number of units you have used: ");  
Scanner sc=new Scanner(System.in);  
unit=sc.nextInt();  
total_bill=PRICE*unit;  
System.out.println("The total amount you have to deposit is: "+total_bill);  
}  
}  

AS a result:

java constant

Constant declared as public:

In the above example, We declare a constant PI as public. Inside the main() method, we have assigned 3.14 in the constants PI. After that, we have invoked the printValue() method. When we execute the program, it throws an error that cannot assign a value to the final variable PI

 public class 
{  
//declaring PI as constant   
public static final double PI= 3.14;  
public static void main(String[] args)   
{  
printValue();  
//trying to assign 3.15 in the constant PI  
PI = 3.15;  
printValue();  
}  
void printValue()   
{  
System.out.print("The value of PI cannot be changed to " + PI);  
}  
}  

As a result:

java constant

For example:

public class ConstantsDemo {
   public static void main(String args[]) {
      final byte var1 = 2;
      final byte var2;
      var2 = -3;
      final short var3 = 32;
      final short var4;
      var4 = -22;
      final int var5 = 100;
      final int var6;
      var6 = -112;
      final long var7 = 20000;
      final long var8;
      var8 = -11223;
      final float var9 = 21.23f;
      final float var10;
      var10 = -121.23f;
      final double var11 = 20000.3223;
      final double var12;
      var12 = -11223.222;
      final boolean var13 = true;
      final boolean var14;
      var14 = false;
      final char var15 = 'e';
      final char var16;
      var16 = 't';
     // Displaying values of all variables
      System.out.println("value of var1 : "+var1);
      System.out.println("value of var2 : "+var2);
      System.out.println("value of var3 : "+var3);
      System.out.println("value of var4 : "+var4);
      System.out.println("value of var5 : "+var5);
      System.out.println("value of var6 : "+var6);
      System.out.println("value of var7 : "+var7);
      System.out.println("value of var8 : "+var8);
      System.out.println("value of var9 : "+var9);
      System.out.println("value of var10 : "+var10);
      System.out.println("value of var11 : "+var11);
      System.out.println("value of var12 : "+var12);
      System.out.println("value of var13 : "+var13);
      System.out.println("value of var14 : "+var14);
      System.out.println("value of var15 : "+var15);
      System.out.println("value of var16 : "+var16);
   }
}

As a result:

value of var1 : 2
value of var2 : -3
value of var3 : 32
value of var4 : -22
value of var5 : 100
value of var6 : -112
value of var7 : 20000
value of var8 : -11223
value of var9 : 21.23
value of var10 : -121.23
value of var11 : 20000.3223
value of var12 : -11223.222
value of var13 : true
value of var14 : false
value of var15 : e
value of var16 : t

Recommended posts

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction

Reference:

tutorialspoint.com

javatpoint.com

Leave a Comment