LightBlog

Java Fundamentals - Types of Variables


1.Instance Variables

  • If the value of a variable is varied from object to object. Such type of variables are called instance variables. For every object a separate copy of instance variables will be created.
  • Instance variables will be crated at the time of object creation and will be destroyed at the time of object destruction.
  • All the Instance variables Stored in “Heap area”
public class Demo
{
    int count = 20;         //1 - Instance variable
}


2.Static Variables

  •  If the value of a variable is fixed for all objects then we have to declare at class level by using static keyword. For the static variables a single copy will be created at class level and shared by all objects of that class.
  • Static variables will be created at the time of class loading and destroyed at the time of unloading.
  • All Static variables are Stored in “Method Area”
public class Test {
        int i;
        static int j = 10;

        public static void main(String arg[]) {
                Test t1 = new Test();
                t1.i = 100;
                t1.j = 200;
                Test t2 = new Test();
                System.out.println(t2.i + " : " + t2.j); //0 : 200
        }
}


3.Local variables

  • If we are declaring a variable with in a method or constants or block such type of variables are called local variables.
  • For the local variables JVM won’t provide any default values. Before using a local variable compulsory we should perform initialization explicitly otherwise compile time error.
public class Test {
        public static void main(String arg[]) {
                int i;
                System.out.println(i);
        }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        The local variable i may not have been initialized

public class Test {
        public static void main(String arg[]) {
                int i;
                System.out.println("Here i not used");//No error
        }
}

  •  It is not recommended to perform initialization of local variables in logical blocks because they is no guaranty of execution these blocks at runtime.
public class Test {
        public static void main(String arg[]) {
                int i;
                if(arg.length>0){
                        i=10;
                }
                System.out.println(i);
        }
}
Test.java:9: error: variable i might not have been initialized
                System.out.println(i);
                                   ^

class Test {
        public static void main(String arg[]) {
                int i;
                if (arg.length > 0) {
                        i = 10;
                } else {
                        i = 20;
                }
                System.out.println(i);
        }
}
//Because if we give arguments 10 will be initialized other wise 20 will be initialized.
It is not good programming practice to perform initialization in logical blocks for local variables because they may not execute at runtime.

  • The only applicable modifier for the local variable is final. If we are using any other modifier we will get compile time error.
public class Test {
        public static void main(String arg[]) {
                public int a;
                protected int b;
                private int c;                 
        }
}
Test.java:7: error: illegal start of expression
                public int a;

  • JVM always calls main method to start the program. Compiler is not responsible to check whether the class contain main() or not. Hence if we are not write the main method , we won’t get any C.E. But at runtime JVM raises NoSuchMethodError:main


Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment