Java variable types Example

By |2017-12-11T07:16:29+05:30April 26th, 2015|java|

In this tutorials we are going to learn different types if variables in Java.

Variables in Java are divided  in to three categories i.e we can create 3 different types of variables in Java those are discussed below.

Java Variable Types :

[tie_list type=”checklist”]

  • instance variables
  • static variables
  • local variables


Variables in Java

Instance Variables

The value of a variable is bound to a particular instance is called instance variable. This value can be varied from one object to another.

Instance variables are allocated memory on heap. Since the instance variables are allocated memory heap, for every object a separate copy of instance variable will be created in the memory.

Instance variables are allocated memory at the time of creating the objects only (Runtime), hence these variables are also called as object level variables.

Instance variables are destroyed, when we destroy the object.

Default values of instance variables :

For instance variables it is not required to perform initialization explicitly. The JVM will provide default values to it.

[tie_list type=”checklist”]

  • In the case of primitives, the default value is zero (0).
  • In the case of object, the default value is ‘null
  • In the case of boolean, the default value is ‘false‘.


Java Instance Variables Example :

public class InstanceDemo {
    public static void main(String[] args) {
        Student s= new Student("chandrashekhar",37);
        System.out.println("name : ""  roll Number: "+s.rool+" is Pass : "+s.isPass);
        Student s2= new Student("raju",4);
        System.out.println("name : ""  roll Number: "+s2.rool+" is Pass : "+s2.isPass);

class Student {

    public Student(String name, int rool) { = name;
        this.rool = rool;
    public void hello(){
         int t = 10;
    String name;
    int rool;
    boolean isPass;


[box type=”success” align=”alignleft” class=”” width=”100%”]
name : chandrashekhar  roll Number: 37 is Pass : false
name : raju  roll Number: 4 is Pass : false

Java Static Variables :

In Java static is a keyword. We can use the static keyword in front of variables, methods, blocks and classes.

Static variables are consider to be class level variables, because the values of the static variables are at class level, and not bound to be a specific instance of that class, where as in instance variables the value of a variable is bound to be with a specific instance.

Where do we use.?

If the value of a variable is not varied from one object to another object, then it is never recommended to declare that variable as reference variable that should be declared as static.

Static variables are allocated memory in static pool, and it is happening while loading the class itself.

Since the static variable are class level variables, we don’t need any instance to access these variables. We can directly access the static variables by using the class name itself.

Example for Java Static variables :

public class StaticVariableDemo {

    public static void main(String[] args) {
        int a = 0;

        Students students = new Students();
        students.setStudentName("chandra shekhar");

        System.out.println("students : " + students.toString() + " College Name : " + Students.getCollegeName());


        Students students2 = new Students();

        System.out.println("students2 : " + students2.toString() + " College Name : " + Students.getCollegeName());

        Students students3 = new Students();

        System.out.println("students3 : " + students3.toString() + " College Name : " + Students.getCollegeName());

        System.out.println("students : " + students.toString() + " College Name : " + Students.getCollegeName());

class Students {

    static String collegeName;
    String studentName;
    int id;
    int standard;

    public static String getCollegeName() {
        return collegeName;

    public static void setCollegeName(String collegeName) {
        Students.collegeName = collegeName;

    public String getStudentName() {
        return studentName;

    public void setStudentName(String studentName) {
        this.studentName = studentName;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public int getStandard() {
        return standard;

    public void setStandard(int standard) {
        this.standard = standard;

    public String toString() {
        return "Students{" + "studentName=" + studentName + ", id=" + id + ", standard=" + standard + '}';


[box type=”success” align=”alignleft” class=”” width=”100%”]

students : Students{studentName=chandra shekhar, id=20, standard=12} College Name : Gayatri
students2 : Students{studentName=Raju, id=21, standard=13} College Name : Geetam
students3 : Students{studentName=Rajesh, id=22, standard=14} College Name : Geetam
students : Students{studentName=chandra shekhar, id=20, standard=12} College Name : Geetam


Do you Know.?

[tie_list type=”starlist”]

  • Static variables can be accessed either by using class name or by using object reference.
  • With in the same class even it is not required to use class name to access static variables, we can access directly.
  • Static variables declared with in the class directly.


Java Local Variables :

A variable which is declared inside a method,block or constructor is called as local variable.

Local variables are allocated memory in stack memory, hence these are also called as stack variables. These will be destroyed from the stack, when completion of method execution.

Local variables are stored in the stack memory hence those are also called as Stack variables or temporary variables.

Hence the scope of the local variables as same as the scope of method or block in which it declared.

Unlike instance variables, for local variables JVM will not provide any default values, we should give the values for local variable before using it.

Do you know.?

  1. The only applicable modifier for the local variable is final.
  2. We can’t apply public, private,protected and static modifiers for local variables.


Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment