?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Constant Variable in Java Programming

    In this lesson, we will look at what is constant variable and how to declare one with the help of some example. We will also play a quiz at the end.

    What is Constant Variable

    A Constant variable in Java is a variable whose value does not change during the execution of the program. Once a value is assigned, in a constant variable, it remains fixed throughout the program. A Constant variable is declared by keyword final.

    Video Explanation

    Click the button below to watch the explanation.

    video image
    Syntax of Declaring Constant Variable in Java
    final datatype variable_name = value;

    Here final is used for declaring Constant, datatype is the name of data type such as int, float, double or char and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).

    Now let's see the example for more understanding.

    Example

    Declare constant variables of each datatype.

    final int a = 23;  		    // a becomes integer constant variable
    final float b = 56.36f;  	// b becomes float constant variable
    final double c = 75.3215;  	// c becomes double constant variable
    final char d = 'k';  		// d becomes character constant variable

    Now the above-declared variables are constant, it means that their values can't be changed during execution of the program.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.