Friday , April 28 2017
Home / java / Java variable types Example

Java variable types Example

Based on the purpose of usage and position of declaration, the Java variable types are divided  in to three categories. By this tutorials we can get to know in detailed explanation about Java Variable types with example programs.

Java Variable Types :

Typically we can divided the Java variable in 3 categories;

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

  • 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 : "+s.name+"  roll Number: "+s.rool+" is Pass : "+s.isPass);
        Student s2= new Student("raju",4);
        System.out.println("name : "+s2.name+"  roll Number: "+s2.rool+" is Pass : "+s2.isPass);
    }
}

class Student {

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

}
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.setCollegeName("Gayatri");

        Students students = new Students();
        students.setId(20);
        students.setStandard(12);
        students.setStudentName("chandra shekhar");

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

        Students.setCollegeName("Geetam");

        Students students2 = new Students();
        students2.setId(21);
        students2.setStandard(13);
        students2.setStudentName("Raju");

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

        Students students3 = new Students();
        students3.setId(22);
        students3.setStandard(14);
        students3.setStudentName("Rajesh");

        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) {
        this.id = id;
    }

    public int getStandard() {
        return standard;
    }

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

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

}

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

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

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+ ,

Check Also

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *