3 minute read

To access services from a Java class, we must firstly instantiate a new instance.

The keyword new creates a new instance of a specified Java class.

Example

This example is based on the class Triangle, which is available in the post

A Quick Guide to Classes, Instances, Properties and Methods in Java.

Create a New Instance

The following statement will

  • create a new triangle instance,
  • initialize the new instance by calling the constructor Triangle() to assign 3, 4 and 5 to three sides, respectively, and
  • store the new triangle to a variable named myTriangle.
Triangle myTriangle = new Triangle(3, 4, 5);

Similarly, changing the argument values in the statement above will customize a different triangle instance.

Create a Collection of New Instances

To make three triangles, simply writing three of such instantiation statement and storing them into three individual variables.

Triangle myTriangle2 = new Triangle(0, 4, 5);
Triangle myTriangle3 = new Triangle(3, -4, 5);
Triangle myTriangle4 = new Triangle(3, 1, 5);

However, it is not preferred to store a collection of objects separately.

An array-like structure is a better choice when storing a collection of objects. The following snippet shows the code by using primitive array type in an array of objects named myTriangles. Here, the object type of each array item is Triangle.

Triangle[] myTriangle = new Triangle[3];
myTriangle[0] = new Triangle(0, 4, 5);
myTriangle[1] = new Triangle(3, -4, 5);
myTriangle[2] = new Triangle(3, 1, 5);

In the above snippet, the first statement,

Triangle[] myTriangle = new Triangle[3];

declares a new array myTriangles which holds references to three Triangle instance objects.

Note that primitive array type has a fixed size. That’s why an integer, 3, is passed here to the array constructor.

The array declaration statement doesn’t create the instances themselves. It allocates three memory units where the address of each Triangle instance may be stored.

The array items have to be created separately, alike the previously-discussed way of creating a single instance. Thus, the three subsequent statements are written to create three Triangle instances and assign them to the array myTriangles with an incrementing index.

Beside primitive array type, there are many other alternatives available in Java from its collection framework.

Collection

A collection is simply an object that groups multiple elements into a single unit/container. The elements to be grouped often belong to the same type. Collections are used to store, retrieve, manipulate and represent aggregate data.

Java Collection Library

Besides primitive array type, Java has the collection framework (JCF) which contains versatile classes for collection data. The library provides classes to implement List, Set, Stack, Queue and Map.

ArrayList is one of the commonly-used resizeable collection structure. Later it will be discussed.

In case you want to explore immediately, here is ArrayList official documentation

Exercise

1.Write a Java class Person.

As Person has been designed in a specific way so that the following requirements should be met:

  • It must be public and have the following private property members: firstName, lastName, gender, email, bDate
  • Person must have a constructor that initializes lastName, firstName and gender for each new Person instance.
  • Person allows outsiders to read its property members.

You can use the same one from the exercise in the post

A Quick Guide to Classes, Instances, Properties and Methods in Java.

2.Write a code snippet which will

  • Create three Person instances.
  • Store the instances in an array.
comments powered by Disqus