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.
- attributes are the data values that define the class or objects of the class. They are defined through instance variables.
- behaviors are what the class or objects of the class can do. They are defined through methods.
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.
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:
- static methods which are methods that belong to the class and should be called using the class name instead of an object.
- instance methods which are methods that belong to the object and are called using an object of the class.
A method has three main parts:
- return type, or
void
, if the method isn't providing a value. A return type is the data type of the value that is the result of calling the method. If the method performs an action and doesn't result in a value, the return type is void
.
- the name of the method. Typically starts with a lowercase letter and adheres to the naming rules for variables.
- parameter list in parenthesis (this list can be empty). The parameter list, similar to a record component list, allows arguments to provide additional data the method needs to complete it's task.
When a record is declared, several instance methods are defined at the same time. There are:
- accessor methods
String toString()
method - returns a String
representation of the object including the values that are assigned to the instance varlables.
boolean equals(Object obj)
method - when comparing two objects of the same type, it returns true
if they contain the same values for the instance variables and false
otherwise.
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:
String year()
- returns the value of the instance variable year
.
String make()
- returns the value of the instance variable make
.
String model()
- returns the value of the isntance variable model
.
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.
- One way that we capture the value, is through assigning a variable to the value returned. The variable needs to be an appropriate type to store the value, so typically the same as the return type.
- The value can also be used in an expression
- The value can also be passed as an argument to another method, including
print
and println
, which will display the value.
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