Skip Top Navigation Bar

Creating and Using Records

Classes

Consider a blueprint for a house. A builder can construct that same style house at various different addresses. The houses, aka objects, each have a distinct address, they might have a different color, different roof material, maybe a different number of bedrooms or bathrooms. But they are all built on the same blueprint and in the same style. Classes are similar to blueprints. They tell the computer how to create an specific object or instance of the class.

Classes are comprised of attributes and behaviors.

Records

In the Learn: Introduction to Records tutorial, you learned how to create a new type by creating a record.

A record is a reference type that allows you to create your own immutable data type where multiple pieces of related data are stored together. We can use one variable to be assigned to a record object rather than using multiple variables.

Record Format

To create a record data type, you need to use the following pattern:

When we create a record, the data values that are being grouped together are listed in parenthesis after the name of the record. Each data value in the list is separated by a comma (,). This list is called a record component list. A record component list contains the data type and variable name for each value included in the record as well as defines a way to access these values. These are the instance variables for the record.

Vehicle Record Example

For example, a record to represent the year, make and model of a vehicle, could look like the following:

record Vehicle(String year, String make, String model) { }

Instance Variables

The instance variables for Vehicle are String year, String make, and String model. These are the data a Vehicle object has.

Declaring and Initializing a Record Object

A specific instance of a record is called an object. Recall, an object is an instance of a class and a record is a type of class. We can declare and initialize many objects or instances of a class.

A constructor is used to create an object and initialize the values for the object. A constructor always has the same name as the class.

When we create a record, we automatically create a constructor to initialize the data in the object to the values in the record component list. For the Vehicle record, the constructor provided is:

public Vehicle(String year, String make, String model) {
   //body of the constructor
}

When creating an object using a constructor, the Java keyword new is used. Since a record is a type of class, we can create an object of a record type using the following pattern:

The values in parenthesis are called arguments. When we create an object or instance of a record, each component in the record component list will need a corresponding argument. These are the values for the components and essentially assigns the component to the value of the argument. The arguments are the same type as the component and are listed in the same order. The arguments are separated by a comma (,). Consider the following example of creating the object myCar.

The arguments "2020", "Toyota", "Camry" are being passed to the components String year, String make, String model as follows:

These values are the values of the instance variables.

Behaviors Defined Through Methods

A method is a block of code that has been defined to perform a specific task or algorithm. Methods can receive arguments making them available to use in the algorithm. Methods can have a return value to provide a result of the algorithm.

Methods can be:

A method has three main parts:

When a record is declared, several instance methods are defined at the same time. There are:

There is an additional method called hashcode; however, it is beyond the scope of this tutorial.

Accessor methods provide the program with the value of the instance variable. With records, the names of these methods are the same as the names of the instance variables. For the Vehicle record, the following accessor methods exist:

Calling Methods

Calling Void Methods

When there is no value being provided as a result of calling the method, the return type of the method is void. These methods are called as follows:

objectName.methodName(argument1, argument2, ...);

If this is a static method, the class name is used instead of the object name. If there are no parameters in the parameter list, the argument list will be empty.

Here is an example call to a void static method from the IO class:

IO.println ("Duke Loves Java");

Calling Non-Void Methods

Non-void methods result in a value. This value can be captured and used.

dataType variableName = objectName.methodName(argument1, argument2, ...);

Here is an example declaration of myCar and a call to the year() method of the Vehicle class in the Java Playground:

toString Method

The String toString() method returns a String representation of the object including the values that are assigned to the instance varlables.

The toString method is automatically called when we attempt to print any object. You will get the same result by running the following program code.

equals Method

The equals method is used to compare two objects. When used to compare two objects of the same type, it returns true if they contain the same values for the instance variables and false otherwise.

Resources