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 data and behaviors.
- data are the values that define the class or objects of the class. They are defined through fields.
- 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.
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 values to be provided to the method. This is 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 fields. With records, the names of these methods are the same as the names of the fields. For the Vehicle
record, the following accessor methods exist:
String year()
- returns the value of the field year
.
String make()
- returns the value of the field make
.
String model()
- returns the value of the field model
.
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 return 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.
There are instances where you might not want to capture or use the return value. In this case, you can call the method like a void
method.
<dataType> variableName = objectName.methodName(argument1, argument2, ...);
Your Turn
Let's try it in the Java Playground.
- Here is an example declaration of
myCar
and a call to the year()
method of the Vehicle
class.
- Add additional calls to the other two accessor methods.
toString Method
The String toString()
method returns a String
representation of the object including the values that are assigned to the fields.
- Write a statement to call the
toString
method on the myCar
object below. Assign the result to myCarDescription
.
- The
toString
method is automatically called when we attempt to print any object.
- Add a statement that prints the
myCar
object without the use of toString
.
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 fields and false
otherwise.
- Add a statement to compare
car1
and car2
.
- Add a statement to compare
car2
and car3
.
Resources
Next Learn Tutorial