Skip to content

Creating an Object

Estimated time to read: 3 minutes

Overview

Objects are created by the new keyword, followed by a reference to the Constructor.

Type of reference Reference Name Assignment keyword Constructor End Statement
Car racerX = new Car() ;
  • Car states the type of the reference we are creating
  • racerX is the name we are using to refer to the instance
  • = is the assignment operator
  • new is a keyword indicating that we want to construct a new object
  • Car() identifies one of the constructors from the Car class and the type of the object we are creating

Two Step Process

Creating an object is a two-step process

First Step - Declaration

The first step creates a reference for an object but does not assign it to an instance, for example:

Car racerX

The above statement declares that we have a Car type reference and it's name will be racerX, but no other details have been assigned to it.

Second Step - new

The second step associates the reference, racerX above, with an object, for example:

racerX = new Car();

The new operation will fill in the address for the attributes and method memory location. It also ensures that the Car class is allocated into memory, that space is made for any required storage and many other requirements by the JVM.

The process of Object Construction

Within the JRE there is a data structure called the reference table, this is a conceptual idea, Java uses this table to keep track of the objects it has constructed.

When a constructor for a class is called:

  • Java looks to see if any instances of this class already exist.
  • If not, it allocates a block of memory to hold the methods of the class.
  • The JRE loads all of the methods such that each methods is located in the memory block.
  • This block of memory will be used to access the methods for every instance of this class.

  • Next the JRE allocates a block of memory to hold the attributes for this instance of the class.

  • The addresses of both of these memory block are then stored in the reference table along with the address of the object.

  • When another instance of this class is created, Java allocates memory for the attributes and simply creates a new entry in the reference table.

  • Java's memory management means that the class is only loaded in fully once.

  • Any additional instances are pointed towards this memory address, instead of having another chunk of memory allocated for a full load. Java will only allocate space for the attributes / data for these new instances.

Reference Table Example

Screenshot 2022-07-25 at 13.06.47.png

Some notes
  • Every object of the same type / class is allocated storage that is sufficient to hold all of its data/primitives. Remember that Java stores all of its data in primitives!
  • Every object of the same type / class must also have a set of its own data / attributes constructed.
  • All objects of the same type / instance points to the same methods, they know their class.
  • An Object is a collection of attributes and methods treated as a single unit. The methods are all shared, they belong to the class.
  • Objects don't get their own methods.