Thursday, April 10, 2014

Practical 2nd : Demo of reference,static and local variable

In last post, I have presented code for literals and primitive type instance variable , Here i will present practical explanation and different scenarios in terms of reference variable, static variable and local variable.
package variableFirst;

1.public class KnowYourVariable {
2. KnowYourVariable myVariable; // it can point to object of KnowYourVariable class 
                            
3. static KnowYourVariable myVariable2; // static variable

4. AnotherReferenceVariable anotherVariable;// it can point to object of AnotherReferenceVariable class only
     
5. AnotherReferenceVariable arv = new KnowYourVariable(); // compiler unhappy

6. public void show() {
7.  System.out.println("printing myVariable:" + myVariable);
8.  System.out.println("printing myVariable2:" + myVariable2);
9.  myVariable = new KnowYourVariable();
 }

10. public static void main(String[] args) {
11.  KnowYourVariable knw;
12.  knw.show(); // compiler unhappy
13.  KnowYourVariable knw = new KnowYourVariable(); // compiler unhappy
14.  myVariable2.show(); //JVM unhappy

15.  KnowYourVariable knw1 = new KnowYourVariable();
16.             knw1.show();
17.             System.out.println(knw1.myVariable);
18.             System.out.println(KnowYourVariable.myVariable2);
 }

}

19.class AnotherReferenceVariable {

}


Output:
printing myVariable:null
printing myVariable2:null
variableFirst.KnowYourVariable@4283df14
null


Explanation:

line no 1: creating a new class.
line no 2: Uninitialized reference variable whose type is KnowYourVariable class.
line no 3: static variable also called as class variable whose type is                      KnowYourVariable class

line no 4: Another instance variable whose type is AnotherReferenceVariable.
line no 5:here compiler is unhappy because reference variable is arv whose type is AnotherReferenceVariable but reference value that it is pointing is KnowYourVariable and there is no relation between KnowYourVariable and AnotherReferenceVariable. so here to make compiler happy arv reference variable should point to the its reference type that is AnotherReferenceVariable. but in case of inheritance and polymorphism this situation can change that we will see in another demo.

line no 6: a method to represent some behavior 
line no 7: here it will print the implicit value of instance reference variable as null.
line no 8: here it will print the implicit value of  static variable as null.
line no 9: here myVariable reference variable will refer to object created of its type that is declared at line no 2.
line no 10: main method to invoke JVM and test the program

line no 11: a local variable of type KnowYourVariable class.
line no 12:compiler is unhappy here because we are using the uninitialized local variable.
line no 13: here we have initialize the variable but compiler is again happy due to duplication of name of variable knw.

line no 14: here compiler will not complain, but java runtime environment will complain ,when we will run the program because here we are using static variable myVariabe2 which points to implicit initialized value that is null. here myVariable2 does not understand whose methods to call, or it does not understand it is pointing to which reference value. so JVM issue NullpointerException means the reference variable you are using to call the method is pointing to null instead of object whose method you want to call.

line no 15: Creating an object of KnowYourVariable class and reference variable knw1
line no 16: properly accessing the method defined in class, here compiler as well as jvm both are happy.
line no 17: printing the value pointed by myVariable by accessing it using dot operator with knw1 reference variable.
line no 18: printing the value pointed by myVariable2 ,  as it is prefixed with static so we can treat it as class variable and we can access it through class name as well as reference variable.


To know the theoretical about this practical follow my post HERE

2 comments:

  1. Nice explanation about variable if you also clear the term property, scope,life then it will be better cause these terms some time confusing for the beginner. Overall good stuff about varibale

    ReplyDelete
    Replies
    1. regarding scope and life-time of variable, i will be posting in upcoming post,keep visiting

      Delete