Tuesday , February 28 2017
Home / java / Java variable types

Java variable types

Based on the purpose and position of declaration the Java variable types are divided  in to three types.

  1. instance variables
  2. static variables
  3. local variables

Variables in Java

Instance Variables

The value of a variable is bound to a particular instance is called instance variable. That value can be varied from one object to another. For every object a separate copy of instance variable will be created in the memory, and the scope of that variable will be the exact scope of the object. Because the instance variables are allocated memory at the time of creating the object and destroy at the time of destroying object. Hence the instance variables are also called as Object level variables.

default values of instance variable.

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

in the case of primitives ‘0’

in the case of object ‘null’

in the case of boolean ‘false’.

 * @author chandrashekhar
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


Static Variables

In Java static variables are class level variables. 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. In Java static can be used in the case of static variables, methods, blocks and class.

In the case of static variable a single copy will be created at class level and those will be loaded the memory at the time of loading the class. Hence the scope of the static variable is exact same of the class. Static variables should be declared with in the class directly by using the static modifier and those can accessible by using class name.

 * @author chandrashekhar
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) {
        this.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 + '}';



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

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

Local Variables

A variable which is declared inside a method or block or constructor is called local variable. Local variables are stored in the stack memory hence those are also called as Stack variables or temporary variables.

Local variables are allocated memory while executing the block or method, in which declared it and destroyed once the block or method completed. Hence the scope of the local variables as same as the scope of method or block in which it declared. For local variables JVM will not provide any default values, we should give the value for local variable explicitly before using.

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.


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

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

Leave a Reply

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