Variables and Constants in Java

In this tutorial, we will be focusing on two important fundamental topics of the Java programming language. We will go through Variables and Constants in Java which are also one of the building blocks of a program.

Variables and Constants in Java

Variables and Constants in Java

Variable in Java

A Variable in Java programming language is used to store a value. The value of a variable can be changed during program execution (runtime). A variable can either be declared without giving it a value or we can create a variable with some initial value.

In Java,

  1. Variable is declared using the following syntax:
    <Data Type> <Variable name>;
  2. Variable is declared and initialized using the following syntax:
    <Data Type> <Variable name> = <Initial Value>;

Example 1:

float average;

In the above example, the variable is named “average” which is not initialized with any value. The data type is considered as “float” so that it will contain floating-point values.

Example 2:

int alphabets = 26;

In the above example, we have a variable which is named “alphabets” and whose value is initialized with a value 26. This value 26 is an integer type of value, so we have the data type of the variable set to “int”.

Types of Variables

There are 3 types of variables in Java. They are,

Local Variables

A variable that is declared within the scope of a function is a local variable. These variables are only accessible within the body of the function where they are declared. The memory allocated to these variables gets released as soon as the function completes its execution.

Example:

void add(int x, int y){
int sum = x+y;
System.out.println("Sum: " + sum);
}

In the above example, variables “x”, “y” and “sum” are local variables whose scope is within the “add” function. These variables cannot be accessed out of the function body.

Instance Variables

These variables are non-static variables that are declared within a class but not in the function of a class. These variables are also member variables of a class.

Example:

class Greet {
String message = "hello World";
            public void greet() {
            	System.out.println(message);
	    }
}

In the above example, a variable “message” is declared and initialized which is a member variable of the Greet class. This variable is neither declared in a function nor is a static variable and that makes it an instance variable.

Static Variables

Static variables are declared within a class using a special keyword “static”. Due to the static nature of these variables, they can be accessed without creating an object of the class when they reside. A static variable can be accessed using the following syntax:

<Class Name>.<Static Variable Name>

Example:

class Human {
       static int eyes = 2;
}
class Main {
       public static void main() {
               System.out.println("We have " + Human.eyes + "eyes");
       }
}

In the above example, class Human contains a static variable “eyes” which is also initialized with a value 2. The static nature of the variable makes it accessible without instantiating an object of Human class. The Main class accesses its value just by using the class name followed by the dot operator and then the variable name (in this case the variable name is ‘eyes’).

Constant in Java

A constant in Java can be a value or a Java variable whose value will not change during the execution of the program.
A constant value can be such as 1100, 12.32, 5000, etc. These values have a fixed meaning that does not change under different circumstances.

A constant variable is a variable that is declared to have a value in it which will be constant till the program ends. Generally, they are declared using a keyword known as “final”. The variables which are declared using the “final” keyword will only be initialized once and then their value will be constant and will not change during the execution of the program.

Example:

class English {
      final int vowels = 5;
}

In the above example. The class English contains an instance variable named “vowels” which is of integer type and is also a “final” type variable. This variable will have a constant value of 5 which cannot be changed during the execution of the program (runtime).

LEAVE A REPLY

Please enter your comment!
Please enter your name here