Last Updated : 30 May, 2018
When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process :
Syntax : class-name var-name; Example : // declare reference to an object of class Box Box mybox;A variable in this state, which currently references no object, can be illustrated as follows (the variable name, mybox, plus a reference pointing to nothing):
Syntax : var-name = new class-name(); Example : // instantiation via new operator and // initialization via default constructor of class Box mybox = new Box();Before understanding, how new dynamically allocates memory, let us see class Box prototype.
class Box { double width; double height; double depth; }A variable after second step, currently refer to a class object, can be illustrated as follows (the variable name, mybox, plus a reference pointing to Box object):
Hence declaration of a class variable, instantiation of a class and initialization of an object of class can be together illustrated as follows :
Important points :Box mybox = new Box();
double height = new Box().height;
int arr[] = new int[5];
Assigning object reference variables
When you assign one object reference variable to another object reference variable, you are not creating a copy of the object, you are only making a copy of the reference. Let us understand this with an example.
Java
// Java program to demonstrate assigning
// of object reference variables
// Box class
class Box
{
double width;
double height;
double depth;
}
// Driver class
public class Test
{
// Driver method
public static void main(String[] args)
{
// creating box object
Box b1 = new Box();
// assigning b2 to b1
Box b2 = b1;
// height via b1 and b2
System.out.println(b1.height);
System.out.println(b2.height);
// changing height via b2
b2.height = 20;
// height via b1 and b2
// after modification through b2
System.out.println(b1.height);
System.out.println(b2.height);
}
}
Output :
0.0 0.0 20.0 20.0Explanation :
First let us understand what the following fragment does in above program.
Box b1 = new Box(); Box b2 = b1;
You might think that
b2is being assigned a reference to a copy of the object referred to by
b1. That is, you might think that
b1and
b2refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes,
b1and
b2will both refer to the same object. The assignment of
b1to
b2did not allocate any memory or copy any part of the original object. It simply makes
b2refer to the same object as does
b1. Thus, any changes made to the object through
b2will affect the object to which
b1is referring, since they are the same object. Same can be verified by output when we change
heightof box via
b2. This situation can be illustrated as follows :
Note :Although
b1and
b2both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to
b1will simply
unhook b1from the original object without affecting the object or affecting
b2.For example :
Box b1 = new Box(); Box b2 = b1; // ... b1 = null;
Here,
b1has been set to
null, but
b2still points to the original object.
Passing object references variables to methods
When we pass object reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. To know more with examples, refer
Passing and Returning Objects in Java.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4