Variables in Java

In this tutorial we will explain you about the variables in Java and types of variable with example

Variable in Java

In this section you will learn about variables in java. First we should know, what is variable? variable is a location in the memory where values persist. In Java, all variable must be declared first before they can used. The basic syntax of declaring variable is as follows:

Data-type identifier = value;

This is the basic syntax of declaring variable in java. You can also declare more than one variable at at time and you can also initialize. To declare more then one variable at a time use comma as a separator. The type is one of data type of java, identifier is the name of the variable based on your choice and value will based on the type of data you declared. There are some rules you have to follow while declaring a variables, they are as follows:

  • A Variable name consists of any combination of alphabets, digits and underscores.
  •  The first character of the variable name must either be alphabet or underscore. It should not start with the digit.
  • No special symbols other than underscore are allowed in the variable name.
  • No commas and blanks are allowed in the variable name.

We need to declare the type of the variable name before  using  of that name in the program.  To declare a variable as integer, float etc follow the  example below:

int p, n;
float r;

Here int is the type of data that variable p and n will hold, intdenotes integer type. You can declare as well as initialize at the time of variable declaration itself.

int a=0,b=9;

There are three kind of variable in java, they are as follows :

  • Instance Variables.
  • class/static variables.
  • Local variables.

Instance variable :

  • Instance variables are declared in a class but outside method or constructor.
  • Instance variables are created when the object is created with new keyword  and destroyed when the object get destroyed.
  • When the space is allocated for object in heap then a slot for each  instance variable value is created.
  • Instance variables are in scope as long as their object in scope.
  • Instance variables are used by all method of a class unless method is marked as static.
  • Access modifier can be given for instance variable.
  • Instance variables have default values, for integer it is 0, boolean it s false and for object reference it is null.
  • Instance variables can be  accessed directly by calling the variable name inside a class.

Example : Use of Instance Variables

class Employe {
	public String name; //Instance variable is visible in any class
    private int salary;  //This Instance variable is visible only in current class
	public Employe(String name,int salary)
     {;   //Name and salary instance variable is assigned in constructor
    public void display() // Displaying the values
    	System.out.println("Employee Name = "+name);
        System.out.println("Employee Salary = " + salary);
public class InstanceVariable
	public static void main(String args[])
		Employe e=new Employe("XYZ",1000);

Output : After compiling and executing of above program

Download Source Code

Static variable :

  • Static is a keyword in java used to create static methods, variable inside a class and static class.
  • Static variables is also called class variable which belongs to class not to object.
  • Static variables is declared inside a class but outside the method or Constructor.
  • These variables are initialized first before initializing of any instance variable.
  • A single copy is shared by a class, as many objects are created.
  • Static variables is accessed without creating object of class using class name.
  • Static variables are created when the program starts and destroys when the program stop.
  • Visibility of class/static variables is same but mostly it is public. Because it is visible for all user in the class.

Example : Use of Static variable

public class Staticvariable 
  static double salary;	 
   int age;
                  public static void main(String args[])
		 Staticvariable sv= new Staticvariable();     // creating object for accessing non static member.
                                     System.out.println("Salary of Employee is="+Staticvariable.salary);  // accessing static variable through class name.
                                     System.out.println("Age of  of Employee is="+sv.age);	// Accessing non static variable through object.	 


Output : After compiling and executing of above program

Download Source Code

Local Variable :

  • Local variables are created within the method or constructor in class.
  • Local variables are exist when the method , constructors exist, and variable will destroyed once the method or constructor exits.
  • Access modifier will not be applied to local variable.
  • Local variables are visible within the method, constructor it is declared.
  • Local variables implemented at stack.
  • While using local variables you have to initialize it first, because local variable has no default value.

Example : A code of program to explain the uses of variables.

class Local {
	public void display() {
		String name = "Rose India";
		String address = "Rohini";
		System.out.println("Name = " + name);
		System.out.println("Address = " + address);
public class LocalVariable {
	public static void main(String args[]) {
		Local ob = new Local();

Output : After compiling and executing of above program

Download Source Code