Local Variable Example Java Program

Definition

Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value. Local variables are visible only in the method or block they are declared. Local variables are created when a method is called and destroyed when the method exits.

Local Variable Example Program

public class LocalVariableDemo{ 
	public void add(){
		int num = 0;
		num = num + 7;
		System.out.println("The number is : " + num);
		System.out.println("This number is inside a method and hence has its scope only inside the method.");
	}
	public static void main(String args[]){
		LocalVariableDemo obj = new LocalVariableDemo();
		obj.add();
	}
}

Sample Output

Output is:
The number is : 7
This number is inside a method and hence has its scope only inside the method.