Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to create a class using utility methods, what is the best approach to do it?

1 Answer

0 votes
by (26.7k points)

You can take the below code as a reference to make a utility method:

public final class ExampleUtilities {

    // Example Utility method

    public static int foo(int i, int j) {

        int val;

        //Do stuff

        return val;

    }

    // Example Utility method overloaded

    public static float foo(float i, float j) {

        float val;

        //Do stuff

        return val;

    }

    // Example Utility method calling private method

    public static long bar(int p) {

        return hid(p) * hid(p);

    }

    // Example private method

    private static long hid(int i) {

        return i * 2 + 1;

    }

}

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Browse Categories

...