Java Reference Variable

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

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
		 */

	}
}
Series Navigation<< Instance Variable in Javapublic static void main(string args[]) Explanation >>
By | 2017-07-09T12:06:20+00:00 April 20th, 2012|Core Java|31 Comments

About the Author:

31 Comments

  1. shiva September 12, 2013 at 10:36 am - Reply

    example please

    • admin September 12, 2013 at 11:22 am - Reply

      Hi Shiva,

      Article has been updated with proper example. Please check again.

      Thanks

  2. bala October 31, 2013 at 6:41 am - Reply

    I want more information about the referance variable. because not got fully. please

  3. prakash bhatnager December 22, 2013 at 4:14 am - Reply

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

  4. Divya May 17, 2014 at 3:38 am - Reply

    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?

    • Basudev September 2, 2016 at 11:12 am - Reply

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

  5. sandeep patidar August 24, 2014 at 12:39 pm - Reply

    is final is a reference variable or not plz suggest
    thanks

    • Vivekanand Gautam August 27, 2014 at 4:00 am - Reply

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

  6. Jack October 27, 2014 at 3:48 pm - Reply

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

    • Vikas kalapur July 13, 2016 at 5:36 pm - Reply

      Can I write like this ?? Is this a valid ??

      Class test {
      Static int i ;
      i=20;
      }

      • J Singh July 14, 2016 at 6:21 pm - Reply

        NO

        • chaitu October 21, 2016 at 11:13 am - Reply

          why???????

          • Shashi November 9, 2016 at 2:56 pm

            U can write…

  7. Ashish singh January 21, 2015 at 9:19 pm - Reply

    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?

  8. Ayesha January 28, 2015 at 6:19 pm - Reply

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

    }

    • Vivekanand Gautam January 31, 2015 at 1:51 pm - Reply

      test class is already defined in above example.

  9. navya April 6, 2015 at 11:06 am - Reply

    what is the difference between static variable and instance variable

    • Pawan Kumar August 16, 2016 at 5:20 am - Reply

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

    • Shashi November 9, 2016 at 3:06 pm - Reply

      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…..

  10. Nagalekshmi April 20, 2015 at 12:39 am - Reply

    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

  11. Nagalekshmi April 20, 2015 at 12:40 am - Reply

    *static

  12. Bhuvan Agarwal October 7, 2015 at 4:34 am - Reply

    Is the value of reference variable sets to null??

  13. uma December 26, 2015 at 5:57 am - Reply

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

    • J Singh December 26, 2015 at 7:48 am - Reply

      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.

  14. kats May 26, 2016 at 1:06 pm - Reply

    what

  15. pawan June 7, 2016 at 3:49 pm - Reply

    sir what type of java knowledge is important for android development

    • J Singh June 8, 2016 at 2:54 am - Reply

      Hi Pawan,

      Core Java is sufficient to start learning Android. There are some parts of Core Java which can be excluded.

      Regards

  16. Santhosh August 14, 2016 at 7:11 pm - Reply

    How to create an object without reference variable? Give 1 example

  17. Santhosh August 14, 2016 at 7:18 pm - Reply

    Please reply as soon as possible

Leave A Comment