Java- Variable Reference



Reference
|
x-- Direct – reference of fields (class-level variables) inside the class
|
x-- Qualified
        |
        x-- Instance ref/Non-static ref – using instance variable of the class.
        |
        x-- Static ref – using class-name itself.

Note: Direct non-static reference should not be made from a static context.



class StaticInstanceVars {
   String msg = "Hello World!";//class-level var.
   public static void main(String[] args) {
        String msg = "Hello World!";//local var.
        System.out.println(msg);

        //qualified static/class ref.
        System.out.println(Rectangle.company);

        //non-static qualified instance ref.
        StaticInstanceVars siv = new StaticInstanceVars();
        System.out.println(siv.msg);

   }
}

class Rectangle {
   public static String company = "Think Force";
   private int length = 0;
   private int breadth = 0;
}

Post a Comment

0 Comments