Scroll to top

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

	}
}
Author avatar
JBT
https://javabeginnerstutorial.com/

38 comments

  1. Bhuvan Agarwal

    Is the value of reference variable sets to null??

  2. uma

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

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

  3. pawan

    sir what type of java knowledge is important for android development

  4. Santhosh

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

  5. Santhosh

    Please reply as soon as possible

  6. 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?

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

  7. sirisha

    i didn’t got correct information about reference variable

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