Scroll to top

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();
		System.out.println("Value after calling method1: " + i);
		System.out.println("Value after calling method2: " + i);

	void method1() {

	void method2() {


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


  1. bala

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

  2. prakash bhatnager

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

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

    • 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

  4. sandeep patidar

    is final is a reference variable or not plz suggest

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

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

  6. Ashish singh

    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?

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


  8. navya

    what is the difference between static variable and instance variable

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

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

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

  10. Bhuvan Agarwal

    Is the value of reference variable sets to null??

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

  12. pawan

    sir what type of java knowledge is important for android development

  13. Santhosh

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

  14. Santhosh

    Please reply as soon as possible

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

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