Sunday, December 19, 2010

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;
}

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...