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:
Follow the below-given syntax to call a user-defined static function:
Example: how to call a user-defined non-static function in Java?
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?
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:
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:
Example: how to call/invoke a predefined non-static Java function?
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 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.