Java Variable Types

  1. Local Variables
  2. Instance Variables
  3. Class (or) Static Variables

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Local Variables

  1. Local variables are declared in methods, constructors, or blocks
  2. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block
  3. Access modifiers cannot be used for local variables
  4. Local variables are visible only within the declared method, constructor or block
  5. Local variables are implemented at stack level internally
  6. There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use
Syntax :

class {

	constructor {
	
		// Use Local Variables within Constructor
	}

	method {

		// Use Local Variables within Method

		block {
	
			// Use Local Variables within Block	
		}	
	}
}

VarType_Local.java

public class VarType_Local {
	
	VarType_Local() {
		
		int a = 1000;
		
		System.out.println("Constructor - Local Variable 'a' : " + a);
	}
	
	void LocalVarAddition() {
		
		int a = 5;
		int b = 10;
		
		int c;
		
		c = a + b;
		
		System.out.println("Method - Local Variable 'a' : " + a);
		System.out.println("Method - Local Variable 'b' : " + b);
		System.out.println("Method - The Addition of Local Variables : " + c);
		
		newline();
		
		for(int i=1; i<=5; i++) {
			
			System.out.println("Block - Local Variable : " + i);
		}
	}	
	
	void newline() {
		
		System.out.println("");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		VarType_Local obj = new VarType_Local() ;
		
		obj.newline();
		
		obj.LocalVarAddition();
	}
}

Sample Output

Constructor - Local Variable 'a' : 1000

Method - Local Variable 'a' : 5
Method - Local Variable 'b' : 10
Method - The Addition of Local Variables : 15

Block - Local Variable : 1
Block - Local Variable : 2
Block - Local Variable : 3
Block - Local Variable : 4
Block - Local Variable : 5




B. Instance Variables

  1. Instance variables are declared in a class, but outside a method, constructor or any block
  2. When a space is allocated for an object in the heap a slot for each instance variable value is created
  3. Instance variables are created when an object is created with the use of the key word 'new' and destroyed when the object is destroyed
  4. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object.s state that must be present through out the class
  5. Instance variables can be declared in class level before or after use
  6. Access modifiers can be given for instance variables
  7. The instance variables are visible for all methods, constructors and block in the class. Normally it is recommended to make these variables private (access level).However visibility for subclasses can be given for these variables with the use of access modifiers
  8. Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor
  9. Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) the should be called using the fully qualified name . ObjectReference.VariableName
Syntax :

BaseClass {

	// Declare Instance Variables Here..

	constructor {

		// Use Instance Variables
	}

	function {

		// Use Instance Variables
		
		block {
		
			// Use Instance Variables
		}
	}
}

SubClass {

	// Use Instance Variables (when instance variables are given accessibility)
}

VarType_Instance.java

public class VarType_Instance {
	
	public int id;
	protected String name;
	private String dept;
	
	VarType_Instance(int x, String y, String z) {
		
		id = x;
		name = y;
		dept = z;
	}
	
	void display() {
		
		System.out.println("Id : " + id);
		System.out.println("Name : " + name);
		System.out.println("Department : " + dept);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		VarType_Instance obj = new VarType_Instance(11, "Priya", "IT");
		
		obj.display();
	}
}

Sample Output

Id : 11
Name : Priya
Department : IT




C. Class (or) Static Variables

  1. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block
  2. There would only be one copy of each class variable per class, regardless of how many objects are created from it
  3. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value
  4. Static variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants
  5. Static variables are created when the program starts and destroyed when the program stops
  6. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class
  7. Default values are same as instance variables. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks
  8. Static variables can be accessed by calling with the class name . ClassName.VariableName
  9. When declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables
Syntax :

BaseClass {

	// Declare Class/Static Variables Here..

	constructor {

		// Use Class/Static Variables
	}

	function {

		// Use Class/Static Variables
		
		block {
		
			// Use Class/Static Variables
		}
	}
}

SubClass {

	// Use Class/Static Variables (when Class/Static variables are given accessibility)
}

VarType_Static.java

public class VarType_Static {
	
	public static final int id = 111;
	public static final String name = "Babe";
	
	protected static int salary = 50000;
	
	void display() {
				
		System.out.println("Id : " + id);
		System.out.println("Name : " + name);
		System.out.println("Salary : " + salary);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		VarType_Static obj = new VarType_Static();
		
		obj.display();
	}
}

Sample Output

Id : 111
Name : Babe
Salary : 50000






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com