Krivalar Tutorials 
Krivalar Tutorials


Java instance variable

<< Local Variable

String to Array>>







Instance variable in Java

Instance variable is a variable declared within the class and without any static keyword

Instance variables are otherwise called as members of a class.

Instance variables are otherwise called as members of a class.


class WoodenFurniture{
	//Instance variables
	String woodGrade;
	float price;
	List list;
	Dimensions dimensions;

	//static variables
	static countInShop;

	public static void main(String args[]){
		//local variable
		int sum=0;
	}
}

In the code above, we have the following instance variables:

  • woodGrade
  • price
  • list
  • dimensions




Instance variables in Java

Instance variable is

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 :/p>

  • 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.

<< Local Variable

String to Array>>

















Searching using Binary Search Tree