Menu
Topics Index
...
`


Classes >
Siva Nookala - 14 Mar 2016
Creating an instance (or object) of Student class (or type) has to be done using the new keyword. This is different when compared to the creation of primitive types.

As discussed in the last topic, we can create an object (or instance) of Student class (or type) as shown below.
Student raja = new Student();
Here raja is the reference which points to the Student object created using the new keyword.

Class, object and reference can be explained better using the analogy of the house plan, house and a paper slip containing house address.
class House
{
    String ownerName;
    int noOfBedrooms;
    double hallWidth;
    double hallLength;
    int noOfToilets;
}
// Create slip, create house and copy the address
House yourAddressSlip = new House();
We can observe the following in the above code.
  • For designing the House Plan, we need to define the class using class House { ... }
  • For creating/constructing the houses we use new House()
  • For creating the address slip, we use House yourAddressSlip
The same word House used in different locations/ways convey different meanings.

Here are some more points we need to understand about House Plan, House and Paper Slip
  • The slip is the reference where as the house is the object. So if we want to use the house we need the slip with address. And slip alone is not useful until it contains the address of house.
// Slip With Address - Construct Raja's house and using the address slip we can go to the house
House rajaAddressSlip = new House();
// Slip Without Address - Not useful since there is no address
House premAddressSlip;
  • A reference when only created and not assigned to any object will be null and is same as slip which is empty. When a new house is built (or created) using new keyword and assigned to a reference, the address of the newly created house is copied into the slip. That is when the slip is useful and can be used to access a house.
// Now useful, since Prem's address slip now points to Raja's house
premAddressSlip = rajaAddressSlip;
  • Another reference can be created, which means we create another slip and also copy the address, so both the slips (or references) point to the same house. We can create as many references for a given house as shown below. Here we have created three references rajaAddressSlip, premAddressSlip, and carpenterAddressSlip, all pointing to the same House. i.e. All the slips contain the same house address and hence refer to the same house.
// Create slip, create house and copy the address
House rajaAddressSlip = new House();
// Create the second slip. This will be null.
House premAddressSlip;
// Copy the address from the first slip to the second slip.
premAddressSlip = rajaAddressSlip;
// Create a third slip and copy the address from first slip.
House carpenterAddressSlip = rajaAddressSlip;
  • If we created a new house, the address on any slip can be changed to that of the new house. So a slip or reference can change address on it but can have only one address at a given time. So slip either points to the old house or the new house.
// Create slip, create first house and copy the address
House rajaAddressSlip = new House();
// Create the second slip and copy the address of first house
House premAddressSlip = rajaAddressSlip;
// Create second house and copy the address into the first slip
rajaAddressSlip = new House();
  • The address on the slip can be erased, so that it does not point to any house. This is also called as nulling the reference (or making a reference as null), so that the reference does not point to any house. Please note that null is a Java keyword.
// Create slip, create first house and copy the address
House rajaAddressSlip = new House();
// Create the second slip and copy the address of first house
House premAddressSlip = rajaAddressSlip;
// Erase the address on the first slip
rajaAddressSlip = null;

The below table gives the summary on references and objects, in comparison with House and paper slip analogy.
House Plan, House and Paper Slip Class, Object and Reference
Using a House Plan, we can create many houses Using a class, we can create many objects
For accessing any house we need an address slip. For accessing any object we need a reference.
Multiple address slips can point to the same house. Multiple references can point to the same object.
The address on the slip can be changed to point to a new house. The reference can be changed to point to a new object.
One slip can contain only one house address at a given time. When a new address is copied, the old address is lost (or overwritten). A reference can point to only one object at a given time. When a new object is assigned to a reference, the old object is gone.
The address on the slip can be erased, so that it does not point to any house. A reference can be nulled (or assigned to null), so that it does not point to any object.
Classes-objects-references The variables (or attributes) inside the Student class like name, marks and section can be accessed using the reference as explained in Member Variable In Java.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App