DEV Community 👩‍💻👨‍💻

Cover image for Custom Method in Java Record Class
luthfisauqi17
luthfisauqi17

Posted on

Custom Method in Java Record Class

Last week I discussed the introduction to the Java Record Class which you can see here. In this article I will discuss how to create a custom method on the record class.

I will take the triangle as an example. I will create a record class Triangle which has two properties namely base and height with both properties having data type double.

Below in Source code 1 you can see the implementation of the Triangle record class.

Source code 1:

public record Triangle(double base, double height) {}
Enter fullscreen mode Exit fullscreen mode

Yes, that's all the Triangle class we need. Let's test the constructor, property accessor, equals, hashCode, and toString of this class as you will see in Source code 2.

Source code 2:

public static void main(String[] args) {
    double base = 4.0;
    double height = 4.0;

        // Objects initialization using constructor
    Triangle triangle1 = new Triangle(base, height);
    Triangle triangle2 = new Triangle(base, height);

        // Property accessor
    System.out.println("Base of triangle1: " + triangle1.base());
    System.out.println("Height of triangle1: " + triangle1.height());

    System.out.println("Base of triangle2: " + triangle2.base());
    System.out.println("Height of triangle2: " + triangle2.height());

        // isEqual method
    System.out.println("Triangle1 and triangle2 is equal: " + triangle1.equals(triangle2));

        // hashCode method
    System.out.println("Hashcode of triangle1: " + triangle1.hashCode());
    System.out.println("Hashcode of triangle2: " + triangle2.hashCode());

        // toString method
    System.out.println(triangle1.toString());
    System.out.println(triangle2.toString());
}
Enter fullscreen mode Exit fullscreen mode

The results of the class test can be seen in Output 1.

Output 1:

Base of triangle1: 4.0
Height of triangle1: 4.0
Base of triangle2: 4.0
Height of triangle2: 4.0
Triangle1 and triangle2 is equal: true
Hashcode of triangle1: 33554432
Hashcode of triangle2: 33554432
Triangle[base=4.0, height=4.0]
Triangle[base=4.0, height=4.0]
Enter fullscreen mode Exit fullscreen mode

In the output above, it can be seen that the test results are in accordance with what we expected, and now we will go to the implementation of custom methods in the record class.


For the implementation of the custom method itself in the record class is not much different from the normal class in Java. We just need to define the method in the record class as in the following example.

Let's say that I want to implement a method to calculate the area of a triangle which I will call getArea() with data type double.

Equation 1 is the formula for finding the area of a triangle.

Equation 1:

area of triangle = (base of triangle * height of triangle) / 2

The implementation of this getArea() method in the code can be seen in Source code 4.

Source code 4:

public record Triangle(double base, double height) {

    public double getArea() {
        return (base * height) / 2;
    }
}
Enter fullscreen mode Exit fullscreen mode

And that's how we implement a custom method on the record class. Next, let's test the method to see if the results will match what we expect or not. Source code 5 shows the testing we did on the getArea() method.

Source code 5:

public static void main(String[] args) {
    double base = 4.0;
    double height = 4.0;

    Triangle triangle = new Triangle(base, height);

    System.out.println("Base of triangle: " + triangle.base());
    System.out.println("Height of triangle: " + triangle.height());
    System.out.println("Area of triangle: " + triangle.getArea());
}
Enter fullscreen mode Exit fullscreen mode

Output 2 shows the results of the tests we did above.

Output 2:

Base of triangle: 4.0
Height of triangle: 4.0
Area of triangle: 8.0
Enter fullscreen mode Exit fullscreen mode

From the output above, we can see that our custom method works well by showing that a triangle with a base of 4 and a height of 4 will have an area of 8. Since:

area of triangle = (base of triangle * height of triangle) / 2

area of triangle = (4 * 4) / 2 = 8


And that's how you create custom methods on record classes in Java. Hopefully this article can help you become more productive when programming using the Java programming language. If you are interested in this Java feature, I strongly encourage you to learn more about the record class via the link below:

https://docs.oracle.com/en/java/javase/15/language/records.html


Cover image:

https://i.picsum.photos/id/443/1920/720.jpg?hmac=lgXcpJtQ_DWNuuVbahKL1siBhn34DfnCCcrn_GmKpnU

Top comments (0)

Create an Account!

👀 Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.