Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
Variables in Java With Examples
In Java, variables are used to store and manipulate data values within a program. There are different types of variables in Java, each with its own characteristics and use cases. In this tutorial, we'll explore the different types of variables available in Java and provide some examples to help you get started.
Variables are containers inside the memory locations, which saves the data during Java Program Execution. A variable is the basic unit of storage in a Java program. A variable is defined as the combination of an identifier, a type and an optional initializer.
1. Declaration and Initialization of Variables:
Declaring a variable is simply creating a variable. The syntax for creating a variable is data_type variable_name = value;. In Java, we can declare the variable first and then later assign some value to it. The values that are assigned to a variable can be changed at anytime during program execution. We should declare a variable before using it. Initialization of a variable is assigning an initial value to the variable while declaring it. For eg: int num=0; here num is the variable_name, int is the data_type and 0 is the initialized value.
Java Variable Declaration Example:
// Example program to declare a variable
import java.io.*;
public class ExampleVar {
public static void main(String[] args) {
int num; // declaring a variable in java
num = 100; // assigning a value to the variable
System.out.println(num);
}
}
Output :
100
2. Type of Variables in Java:
There are three types of variables in Java, which are following:
1. Local Variables:
A local variable is a variable that is declared inside the body of a method, constructor, or block. Local variables are only accessible within the scope in which they are declared and are destroyed once the method, constructor or block in which they were declared has completed execution. Local variables do not have default values and must be initialized before they can be used.
Here's an example of how to declare and use a local variable in Java:
// Declaring and using a local variable
public class LocalVariableExample {
public static void main(String[] args) {
int num; // Declaring a local variable
num = 100; // Initializing the local variable
System.out.println(num); // Printing the value of the local variable
}
}
2. Instance Variables:
Instance variables are variables that are declared within a class, but outside the body of any method, constructor, or block. Instance variables are associated with a specific instance of a class and are created when an object of that class is created using the new
keyword. Instance variables hold values that are specific to an instance of an object and are accessible from any method, constructor, or block within that object.
Here's an example of how to declare and use instance variables in Java:
// Declaring and using instance variables
public class InstanceVariableExample {
String name; // Declaring an instance variable
int age; // Declaring another instance variable
public static void main(String[] args) {
InstanceVariableExample obj = new InstanceVariableExample(); // Creating an object of the class
obj.name = "John Smith"; // Initializing the instance variables of the object
obj.age = 30;
System.out.println("Name: " + obj.name + ", Age: " + obj.age); // Printing the values of the instance variables
}
}
3. Static Variables:
Static variables, also known as class variables, are variables that are declared with the static
keyword within a class, but outside the body of any method, constructor, or block. Static variables are associated with a class as a whole, rather than with a specific instance of that class. There is only one copy of a static variable shared by all instances of a class. Static variables are created when the class is loaded and are destroyed when the class is unloaded.
Here's an example of how to declare and use static variables in Java:
// Declaring and using static variables
public class StaticVariableExample {
static int num; // Declaring a static variable
public static void main(String[] args) {
StaticVariableExample obj1 = new StaticVariableExample(); // Creating an object of the class
obj1.num = 100; // Initializing the static variable of the object
StaticVariableExample obj2 = new StaticVariableExample(); // Creating another object of the class
obj2.num = 200; // Initializing the static variable of the object
System.out.println("Value of num in obj1: " + obj1.num); // Printing the value of the static variable for obj1
System.out.println("Value of num in obj2: " + obj2.num); // Printing the value of the static variable for obj2
}
}
In the example above, the static variable num is shared by both objects obj1 and obj2, so when we initialize the value of num for one object, it changes for both objects.
Apart from the above three variable types, we can also make a variable value constant (Assigned Value can't be changed in future). These type of variables are call Final Variables.
3. Final Variables in Java:
Final variables are variables that are declared with the final keyword and cannot be changed once they have been initialized. Final variables can be either local, instance, or static variables. Final variables are useful for creating constants that cannot be modified by the rest of the program.
Here's an example of how to declare and use final variables in Java:
// Declaring and using final variables
public class FinalVariableExample {
final int num = 100; // Declaring and initializing a final instance variable
final static double PI = 3.14159; // Declaring and initializing a final static variable
public static void main(String[] args) {
final int localNum; // Declaring a final local variable
localNum = 200; // Initializing the final local variable
System.out.println("Value of final instance variable: " + new FinalVariableExample().num); // Printing the value of the final instance variable
System.out.println("Value of final static variable: " + PI); // Printing the value of the final static variable
System.out.println("Value of final local variable: " + localNum); // Printing the value of the final local variable
}
}