N
Glam Journal

What is a reference variable in Java with an example?

Author

William Burgess

Updated on March 06, 2026

What is a reference variable in Java with an example?

A variable that holds reference of an object is called a reference variable . For example, If we create a variable to hold a String object, then it is called reference variable because String is a class. …

What is reference variable explain with an example?

A reference variable is a variable that points to an object of a given class, letting you access the value of an object. For example, you can retrieve a row from a database table and assign all values from the row to a single object and then pass the object to a called procedure.

What is reference variable Java?

A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables. The data within the object can be modified, but the reference variable cannot be changed.

Why do we use reference variable in Java?

Reference variable is used to point object/values. 2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java.

What is reference variable How do you create it?

Creating references in C++ The basic syntax to create a reference is – Data type& new variable = previous variable. The newly created variable will now refer to the previous variable. For example – int i = 17 // The variable i is declared as 17.

Can reference variables be final?

A reference variable declared final can never be reassigned to refer to an different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.

How do you create a reference variable?

When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.

What is the difference between object and reference variable in Java?

What is difference between references and objects in java? A reference is an entity which provides a way to access object of its type. An object is an entity which provides a way to access the members of it’s class or type. Generally, You can’t access an object without a reference to it.

Is object a reference variable?

In short, object is an instance of a class and reference (variable) points out to the object created in the heap area.

How do you pass a reference variable in Java?

Approach:

  1. Get the integer to be passed.
  2. Create an MutableInt object by passing this integer as parameter to its constructor.
  3. Now whenever you need the integer, you have to get it through the object of the MutableInt.
  4. Hence the Integer has been passed by reference.

Where are reference variables stored in Java?

All objects in Java are stored on the heap. The “variables” that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also. The Class objects that define Classes are also heap objects.

Can a reference be modified?

The data with in object can be modified but the reference variable cannot be changed.

What are the types of variables in Java?

Variable is a name of memory location. There are three types of variables in java: local, instance and static. There are two types of data types in java: primitive and non-primitive. Variable is name of reserved area allocated in memory.

What is primitive and reference variable in Java?

Variables in Java are classified into primitive and reference variables. From the programmer’s perspective, a primitive variable’s information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable. reference variables are practically always objects in Java.

What is difference between variable and object in Java?

An object is a real entity which has properties and methods. A variable may contain the reference to an Object. In JavaScript, variables are objects that are “containers” for storing information. Objects are with all of their properties, methods, and event handlers.

What are the local variables in Java?

A local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists.