Java Reference Variable

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

Types of reference variables

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

Static Variable Example

class test {
	//below variable is static variable means it is class level variable
	static int i;

	public static void main(String[] args) {
		// As i is an static variable it can be accessed directly without using any object
		System.out.println("Value before calling method1: " + i);
		test t1 = new test();
		t1.method1();
		System.out.println("Value after calling method1: " + i);
		t1.method2();
		System.out.println("Value after calling method2: " + i);
	}

	void method1() {
		i++;
	}

	void method2() {
		i++;
	}

}

Instance / Local / Method Parameter Example

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

	}

	/*
	 * Here j is a method parameter. And k is a local variable.
	 * 
	 * Note**: Local variables life is only till the end of method
	 */
	void method1(int j) {
		int k;
		i = j;
		/*
		 * Local Variable(k)'s life ends once execution for this method
		 * completes. As k is local is variable it needs to be initialized 
		 * before we can use it. But as it is not getting used here, it can stay here without initializing
		 */

	}
}

40 Comments Java Reference Variable

  1. Rupal Mukhi

    Can’t I use a reference variable inside the main method?
    for eg:
    public class Test
    {
    public static void main(String args[])
    {
    Test t; //reference variable
    System.out.println(t);
    }
    }
    what is the problem with this example?

    Reply
    1. JBT

      No. Main is the static method. And static methods cannot use an instance variable. Static methods are not bound to any instance. While instance variables are specific to instances.

      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
  2. 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
    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
  3. 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
  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. 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
    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
  6. 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

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.