Read Java Interview Questions and Answers for Beginners and Experienced

Java Constant Examples

In this Post of Java tutorial of Java examples, we will discuss about Java constants examples by studying Java constants.

What is Constant?

So what is constant? before reading Java constants, let’s read what a constant first.

Constant is anything who remains constant once created. Constant never change once declared. Never changing is the property of the constant.

example: pie (π) value remain constant which is 3.14.

 

What is Java Constant?

As we read about constants. constants are those who never change its value once declared.

Constant in Java are those constants who never change its value once created.

 

Java constants are those constants whose value does not change throughout the Java program once created. It act like static value who never change during the execution of the Java program.

In Java, we can not create constant just to declare any variable as constant. There is no keyword constant in Java or we can say there is no direct way to create constant in Java then Let’s see how to create Java constants.

 

How to Create Java Constant?

As we study that there is no direct way to create Java constant, so to make any variable as Java constant we have to follow:

  • To make any variable as Java constant,first declare that variable as <strong>final</strong>

like

final int AREA=200;

purpose of the final keyword is to make the value as final, we can not change the value of variable AREA which is 200.

Now, technically we have created Java constant by just putting final keyword before the variable.

But, for Optimizing the Java Constant, we also use of static keyword like this

static final int AREA=200;

 

Syntax of Java Constant

So above we saw that Java constant declare with static and  final keyword, so Java constant  syntax will be

static final data_type variable_name= constant_value;

 

  • Here static and final are two keywords.
  • data_type is the datatype of the constant. It could be int, String, double any data type.
  • variable_name is the name of the constant variable.
  • constant_value is the constant value of Java constant.

 

Why we need Java Constant?

Now, Question arise why we need Java constant? There are couple of things due to that Java constant is created:

As Java constant can never be change, so due to security related things, once assigned the value, no one can change in between.

 

Rules to follow while creating Java constant

There are some rule to follow while creating constant in Java programming language:

  • Variable will be declared as final and static :

Java constant will be declared with static and final keywords, so that they can not be changed once value is assigned.

  • Java constant will be declare in upper case

There is a rule or nomenclature to follow while creating constant in java that constant will be declared in upper case, so that it can be easily identifiable in Java program like

static final  String WEBSITE="ByteArray.in";

 

Java constant example

Now we have enough understanding on Java constant. we read about Java constant, discussed syntax of Java constant, lets see Java constant example.


public class JavaExamples {

static final double PIE= 3.14;
public static void main(String[] args) {
//Java constant Example in Java area of circle by ByteArray.in

JavaExamples obj=new JavaExamples();


System.out.println("Area of circle having Radius 10 is :"+obj.areaOfCircle(10));

}

public double areaOfCircle(int r) {

return PIE *r*r;

}

}

 

output

Area of circle having Radius 10 is :314.0

 

Explanation

In above Java constant example, we are calculating the area of a circle. we have declared one Java constant PIE whose value we assigned as 3.14. we declare Java constant outside the main method, because static variables are class level variables.

We created one method to calculate the area of a circle. We have created one object of class to access the method

 

 

How to create integer type Java constant

To create a integer or numeric type Java constant, you just need to change data type to int, see below integer type java constant example

 


public class JavaExamples {

//create integer type java constant example by byteArray.in 
static final int VOTING_AGE= 18;
public static void main(String[] args) {

System.out.println("Minimum Age of voting in India is: "+VOTING_AGE);

}

}

output

Minimum Age of voting in India is: 18.0

 

Explanation

In above integer type java constant example, we created VOTING_AGE  Java integer constant.

 

How to create String type Java constant

Now we will see how to create String type java constant. Sting type Java constant is similar to integer type Java constant, just need to change the value and data type to String. See below String  java constant example


public class JavaExamples {

//create String type java constant example by byteArray.in 
static final String COUNTRY= "INDIA";
public static void main(String[] args) {

System.out.println("Country name is "+COUNTRY);

}

}

 

output

Country name is INDIA

 

If there is need of creating any Java constant, just need to change the data type of that constant and the value accordingly.