Java Reference Variable

This entry is part 8 of 34 in the series Core Java Course

Reference variables are used to refer to an object. They are declared with a specific type which cannot be changed.

Types of reference variables

  • Static Variable
  • Instance Variable
  • Method Parameter
  • Local Variable

 

Static Variable Example

Instance / Local / Method Parameter Example

Series Navigation<< Instance Variable in Javapublic static void main(string args[]) Explanation >>

31 Comments Java Reference Variable

  1. prakash bhatnager

    A reference is what is used to describe the pointer to the memory location where the Object resides

    Reply
  2. Divya

    sir,cud u pls let me knw : how a static variable can be accessed? To be clear,is it accessed via an obj or without any object?

    Reply
    1. Basudev

      //Static variable can accessed using Class name or using an instance of class object //example :
      class Test
      {
      static int var;

      public static void main (String[] args)
      {
      //————–Example1 using class name—————-:
      Test.var = 5;
      System.out.println(Test.var); // output =5

      //————/Example2: Using Class instance:—————
      Test objTest = new Test();
      objTest.var =10;
      System.out.println(Test.var); // output =10
      }
      }

      Reply
    1. Vivekanand Gautam

      Final is a keyword work as Non Access Modifier. It is not a variable in itself. But can be applied to Instance Variable.

      Reply
  3. Jack

    In the second example, the class name has changed from ‘test’ to ‘MPE’, but on line 9, we see this:
    test t1 = new test();

    Shouldn’t the creation of the t1 object look like this:
    MPE t1 = new MPE();

    Reply
  4. Ashish singh

    sir,
    i m a java beginner.I just want ask u that can we change the value of variable which is declared as static and why static keyword is used?

    Reply
  5. Ayesha

    class MPE {
    // Instance Variable
    int i;

    public static void main(String[] args) {
    /*
    * Here i is an Instance variable.
    */
    test t1 = new test();
    System.out.println(“Value before calling method1: ” + t1.i);

    }

    The above program refers a class test which is actually not a class name.
    it should be as
    class MPE {
    // Instance Variable
    int i;

    public static void main(String[] args) {
    /*
    * Here i is an Instance variable.
    */
    MPE t1 = new MPE();
    System.out.println(“Value before calling method1: ” + t1.i);

    }

    Reply
    1. Pawan Kumar

      Hi NAVYA,
      The Static variable is nothing but preceded by static keyword and its also called class variable whenever .class file is loaded autocratically JVM will allocate the memory for all static member ,and its perform in method area ,
      —> you can access this var to four ways :-
      Ex:-
      class A{
      static int number = 10;
      psvm(Strin args[]){
      A ob = new A();
      }
      }
      1) direct // S.o.p(number);
      2) by class name // S.o.p(A.number);
      3)by object // S.o.p(new A.number);
      4)by object reference // S.o.p(ob.number);
      —> but there two ways is not commanded 3>by object and 4>by object reference

      ii> Instance variable — the instance variable is nothing but member of class and it define as out side of method ,block and constructor
      and memory will allocate for this variable whenever object is created and will destroyed when object is destroy .
      it is two ways to access ;-
      1)by object // S.o.p(new A.number);
      2)by object reference // S.o.p(ob.number);

      Reply
    2. Shashi

      Static variable belongs to class not object where as instance variable refers to object..
      When you call a non-static variable using object…the value of non- static variable remains same until the object is destroyed…
      and when next object calls the same instance variable the default value assigned by u is available to the non-static variable..

      In static variable with every changed made to the value of static variable the new value is replaced by the old value….
      when obj1 calls static variable and make changes to it…this change is stored at the memory location of static variable…
      when obj2 calls static variable the new value (made by obj1) is used by obj2..

      STATIC variable is only allocated memory once since it belongs to class and when class is loaded the memory is allocated to static variable…
      NON-STATIC variable is allocated memory every time when new object is created since it belongs to object not class…..

      Reply
  6. Nagalekshmi

    Hi Navya,
    Static variables will have the keyword status meaning it belongs to the class,it is a common variable shared by all instance of the class.so if you like access such a variable you can use any of the object of the same class or the class name itself.

    Instance variable alike are instance specific. So every instance of the class has a unique instance variable.they must be accessed by the same instance only

    Reply
  7. uma

    As you said, local variables must be initialized. But you did not initialize the local variable “k”. Am I right?

    Reply
    1. J Singh

      Statement is correct in sense you can not use any local variable unless and until you initialize it. But you can have instance variable without initializing it if you are not using it. This is the case in this example. k is local variable without initializing.

      Reply

Leave A Comment

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