Krivalar Tutorials 
Krivalar Tutorials


Java local variable

<< static variable

instance variable >>







Local variables in Java

Local variable scope is local to the block of code within which it appears.

Local variables are simple variables declared within a block of code.

Local variables are declared :

  • within static void main(String a[]) method
  • within any method
  • within for loop, within while loop, within do while loop
  • within if or else block
  • within switch case block
  • within synchronized block
  • within a block in between braces { and }

Local variables are not static. Local variables are not instance variables.






import java.util.List;
import java.util.ArrayList;

public class MyExample{
public static void main(String args[])
{
	int countOfEmployees=0;
	char flag='C';
	float totalRevenue=0.0;
	List listOfElements=new ArrayList();
	list.add("One");
	list.add("Two");

	System.out.println("countOfEmployees:"+ countOfEmployees);
	System.out.println("flag:"+ flag);
	System.out.println("totalRevenue:"+ totalRevenue);
	System.out.println("list:"+ listOfElements.toString());

}
//The below would not compile and hence commented
//	System.out.println("countOfEmployees:"+ countOfEmployees);
//	System.out.println("flag:"+ flag);
//	System.out.println("totalRevenue:"+ totalRevenue);
//	System.out.println("list:"+ listOfElements.toString());

}

Values stored in local variable can be accessed or modified or updated only in the block in which it is declared.

<< static variable

instance variable >>

















Searching using Binary Search Tree