| by Arround The Web | No comments

How to call a function in Java

In Java, a function is a set of statements that executes a specific task/operation when someone invokes it. Functions/methods are popularly used concepts in any programming language that facilitates us with the reusability of the code. In a java program, each function must have a unique name. Every function will be called/invoked with its name. Once a user invokes a function the code associated with that function will get executed.

This write-up aims to present a thorough overview of the below-given learning outcomes:

So, let’s get started!

How to call a user-defined Java function?

In java, the dot. The syntax is used to call/invoke a user-defined function. The below-given syntax will assist you to understand how to call a user-defined non-static java function:

classObject.methodName();

Follow the below-given syntax to call a user-defined static function:

methodName();

Example: how to call a user-defined non-static function in Java?

public class ExampleClass {
    void showInformation() {
        System.out.println("This is a user-defined non-static function");
    }

    public static void main(String[] args) {
        ExampleClass exObj = new ExampleClass();
        exObj.showInformation();
    }
}

In the above-given program, we created a non-static function named “showInformation()”. In java, we can’t call a non-static method directly, so, first, we created an object of the class. Afterward, we utilized that object to call the “showInformation()” method:

The output verified that the user-defined method was invoked successfully using the class object.

Example: how to call a user-defined static function in Java?

public class ExampleClass {
    static void showInformation() {
        System.out.println("This is a user-defined static function");
    }

    public static void main(String[] args) {
       showInformation();
    }
}

In the above-given program, we created a static function named “showInformation()”. In java, a static function can be invoked without creating the class object. So, we invoked the “showInformation()” function directly, without creating the class object:

This is how you can call a user-defined static function in Java.

How to call a predefined Java function?

Java offers numerous predefined classes such as Math class, Scanner class, System class, and so on. All these classes offer multiple build-in methods that serve different functionalities. For example, the Java string class provides several built-in functions that are used to perform various tasks. Such as the length() method is used to get the string’s length(number of characters), concat() is used to concatenate two strings, sqrt() is used to find the square root of a value, and so on.

To call a predefined non-static java function, create a class object and then utilize the dot “.” syntax to call the function:

classObject.functionName();

To call a predefined static function, simply type the class name followed by a dot and a function name as shown in the below snippet:

className.functionName();

Example: how to call/invoke a predefined non-static Java function?

public class ExampleClass {
    public static void main(String[] args) {
        String message = "Welcome to linuxhint.com";
        System.out.println("Total number of characters: " + message.length());
    }
}

In this example program, we utilized a non-static length() method of the string class to find the total number of characters present in the given string:

The output verified that the length() method returned the number of characters present in the given string, including white spaces.

Example: how to invoke/call a predefined static java function?

public class ExampleClass {

 public static void main(String[] args) {
  int number1 = 12;
  int number2 = 2;
  System.out.println("12 raised to the power 2: " + Math.pow(number1, number2));          
 }
}

In this program, we utilized a static method named pow() of the Java Math class. The pow() function is a static method, so we didn’t create the class object:

In this way, we can call any built-in static function in Java.

Conclusion

To call a user-defined static java function, type the function name followed by a set of parentheses and a semicolon. To call a predefined static function, simply type the class name followed by a dot and a function name. To call a user-defined or predefined non-static java function, create a class object and then follow the dot “.” syntax to call the function. This write-up considered a couple of examples to explain how to call a predefined or user-defined function in Java.

Share Button

Source: linuxhint.com

Leave a Reply