- Reference Variable in Java
- Java
- Java
- Java
- Java
- More on Reference Variable
- What is reference variable in Java?
- Java Reference Variable
- Java Reference Variable
- What do we mean by Reference Variable?
- Primitive Variable vs Reference Variable
- Types of Java Reference Variable
- Example of Reference Variable Being Created
Reference Variable in Java
Before We Started with the Reference variable we should know about the following facts.
1. When we create an object (instance) of class then space is reserved in heap memory. Let’s understand with the help of an example.
Now, The space in the heap Memory is created but the question is how to access that space?.
Then, We create a Pointing element or simply called Reference variable which simply points out the Object(the created space in a Heap Memory).
Understanding Reference variable
1. 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.
3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value.
4. You can access object members using a reference variable using dot syntax.
Java
Let us see what is actually happening step by step.
1. When we create an object of demo class new DEMO();, the default constructor is called and returns a reference of the object, and simply this reference will be stored to the reference variable D1 (As we know that associativity is Right-hand side to left-hand side).
2. The value of a reference variable is a reference. When we attempt to print the value of a reference variable, the output contains the name of the class which has been instantiated concatenated by @ and the hash code created for it by Java: the string Demo@214c265e tells us that the given variable is of type Name and its hexadecimal format of hash code is 214c265e.
3. At this point we will access the methods display() of the class demo using our custom reference variable that we created.
BINDING UP : The constructor call returns a value that is a reference to the newly-created object. The equality sign tells the program that the value of the right-hand side expression is to be copied as the value of the variable on the left-hand side. The reference to the newly-created object, returned by the constructor call, is copied as the value of the variable.
Java
Java
Java
Note:
Here we pass G1 and Q1 reference variable point out the same object respectively. Secondly At Point 1 we try to get the value of the object with G1 reference variable which shows it as 25 and At Point 2 we try to get the value of an object with D1 reference variable which shows it as 25 as well. This will prove that the modification in the object can be done by using any reference variable but the condition is it should hold the same reference.
More on Reference Variable
1. Reference Variable as Method Parameters:
As the value of a primitive variable is directly stored in the variable, whereas the value of a reference variable holds a reference to an object. We also mentioned that assigning a value with the equality sign copies the value (possibly of some variable) on the right-hand side and stores it as the value of the left-hand-side variable. A similar kind of copying occurs during a method call. Regardless of whether the variable is primitive or reference type, a copy of the value is passed to the method’s argument and copied to that argument.
Note: Java only supports pass by value.
But we know that the reference variable holds the reference of an instance(OBJECT) so a copy of the reference is passed to the method’s argument.
What is reference variable in Java?
The only way you can access an object is through a reference variable. 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.
A reference variable that is declared as final can’t never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.
package org.kodejava.basic; public class ReferenceDemo < public static void main(String[] args) < // Declaration of Reference variable Reference ref1, ref2; // ref3 is declared final, ref3 can't be reassigned // or refer to different object final Reference ref3; // assign ref1 with object Reference ref1 = new Reference("This is the first reference variable", 1); // access method getNumber() of object Reference through // variable ref1 int number = ref1.getNumber(); System.out.println("number= " + number); // assign ref2 with object Reference ref2 = new Reference("This is the second reference variable", 2); // passing ref2 as method parameter of printText() method ReferenceDemo.printText(ref2); // assign ref3 with object Reference ref3 = new Reference("This is the third reference variable", 3); // try to reassign ref3 will cause a compile-time error // ref3 = new Reference("Try to reassign", 3); >public static void printText(Reference reference) < String text = reference.getText(); System.out.println(text); >>
package org.kodejava.basic; public class Reference < private int number; private String text; Reference(String text, int number) < this.text = text; this.number = number; >public String getText() < return text; >public int getNumber() < return number; >>
As you can see, learning Java is rather easy, as it is a well-structured programming language with many automated processes. All you need is dedication and a little free time. If you are still in school, a write my essay service can definitely help you with the latter.
A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏
Java Reference Variable
We often are stuck at discussion about Objects and their references, it is known fact that unlike C or C++, java doesn’t allows variable access using pointers, well why it does not allows is itself a whole topic which you can read here. Today we will discuss about what all types can be given to java variables or objects. Before moving on to that concept let’s have a look at what do we mean by Reference Variable?
Java Reference Variable
What do we mean by Reference Variable?
A reference variable is used to access the object of a class. Reference variables are created at the program compilation time.
Primitive Variable vs Reference Variable
Having seen about reference variable above, let’s have basic understanding about the types of variables, i.e. Primitive Variables and Reference Variables. All the variables created using Primitive data types (char, int, boolean, float, double, short and long) are treated a bit differently by JVM in comparison to the ones which are used to point objects like the famous String class, File Objects, Thread objects and so on. If we talk about in terms of memory allocation, Reference Variables are the objects handles to the class references which are created in the Heap Memory.
Now let’s move on to discuss about Reference Variables.
Types of Java Reference Variable
- Local Variable – Block Specific Or Method Specific Variables
- Static Variable – The class Variables
- Instance Variable – The Non Static Variables
- Method Parameter
Example of Reference Variable Being Created
In the below example we will take the case of an Employee class which has few primitive variables and we would take the reference of the Employee Class as “emp” and then explain its further working: