Types of Variable in Java :

Variable is a name of memory location. Variable is the name of reversed area allocated in memory ,It is a combination of “vary + able” that means its value can be changed. 

 In each variables having a specific types and its determine the size and layout ,the range of the memory that can be store within that memory  and the set of variables that can be applied to the variables .

The basic form of a variable declaration is given below:

data type variable [ = value][, variable [ = value] …] ;

Types of Variable in Java

Example

int a, b, c;         // Declares three ints, a, b, and c.

int a = 10, b = 10;  // Example of initialization

byte B = 22;         // initializes a byte type variable B.

double pi = 3.14159; // declares and assigns a value of PI.

char a = ‘a’;        // the char variable a iis initialized with value ‘a’

 

This Chapter can explain us various types of  variable available in Java.

There are three kinds of variables in Java −

Types of Variable in Java

Local variable:

If are declaring a variable with in a method  or  constructor  or block  such types of variable is called Local Variable.

  • Local variable are also Known as Temporary variable or automation variable .
  • Local variables will be created as the part of method execution and will be destroyed once the method completes.
  • For the local variable JVM Won’t provide the default value. Before using the local variable compulsory we should initialization  explicitly otherwise time error.

 Example  :

public class Test {

public void pupAge() {

int age = 0;

age = age + 7;

System.out.println(“Puppy age is : ” + age);

}

public static void main(String args[]) {

Test test = new Test();

test.pupAge();

}

}

Output

Puppy age is: 7

Example

Following above example uses age without initializing it, so it would give an error at the time of compilation.

public class Test

{

public void pupAge()

{

int age;

age = age + 7;

System.out.println(“Puppy age is : ” + age);

}

public static void main(String args[]) {

Test test = new Test();

test.pupAge();

}

}

Output

age = age + 7;

^

1 error

Instance Variable:

If the value of the variable is varied from object to object .Such types of variable are called instance variable. For each  object separate copy of  instance variable  created.

  • Instance variable will be created at the time of object creation and will be destroyed at the time of object i.e the scope of instance variable is exactly same as the scope of object .
  • For the instance variable no need to perform intialization .JVM will always provide values.
  • Instance Variable are declared in class but in outside a method ,constructor.

Example

import java.io.*;

public class Employee

{

public String name;

private double salary;

public Employee (String empName) {

name = empName;

}

public void setSalary(double empSal)

{

salary = empSal;

}

public void printEmp() {

System.out.println(“name  : ” + name );

System.out.println(“salary :” + salary);

}

public static void main(String args[])

{

Employee empOne = new Employee(“Ransika”);

empOne.setSalary(1000);

empOne.printEmp();

}

}

This will produce the following result −

Output

name  : Ransika

salary :1000.0

Class/Static Variable :

 Static Variable : If the value of variable is fixed for all the object then it is not recommended to declare that variable at instance level. Such types of variable we have to declare at class level by using static keyword.

  • For a static variable a single class will be created at class level and shared by all objects of that class static variable should be declared with in the class but outside of any method or block constructor .
  • static variable should be declared with in the created at class but outside of any method or block or constructor .
  • We can access static variable either by using class name or by using object reference using name is recommended.

staticExample

import java.io.*;

public class Employee

{

private static double salary;

public static final String DEPARTMENT = “Development “;

public static void main(String args[]) {

salary = 1000;

System.out.println(DEPARTMENT + “average salary:” + salary);

}

}

This will produce the following result −

Output

Development average salary:1000

Recommended Reading :