How to Call a Method

JavaJavaBeginner
Practice Now

Introduction

Methods in Java are a group of tasks that perform a specific action. In this lab, we will learn how to call these methods in Java. As part of this lab, you will learn how to call inbuilt and user-defined methods in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/math("`Math`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/math_methods("`Math Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117413{{"`How to Call a Method`"}} java/classes_objects -.-> lab-117413{{"`How to Call a Method`"}} java/class_methods -.-> lab-117413{{"`How to Call a Method`"}} java/modifiers -.-> lab-117413{{"`How to Call a Method`"}} java/oop -.-> lab-117413{{"`How to Call a Method`"}} java/identifier -.-> lab-117413{{"`How to Call a Method`"}} java/arrays -.-> lab-117413{{"`How to Call a Method`"}} java/comments -.-> lab-117413{{"`How to Call a Method`"}} java/data_types -.-> lab-117413{{"`How to Call a Method`"}} java/math -.-> lab-117413{{"`How to Call a Method`"}} java/operators -.-> lab-117413{{"`How to Call a Method`"}} java/output -.-> lab-117413{{"`How to Call a Method`"}} java/strings -.-> lab-117413{{"`How to Call a Method`"}} java/variables -.-> lab-117413{{"`How to Call a Method`"}} java/math_methods -.-> lab-117413{{"`How to Call a Method`"}} java/system_methods -.-> lab-117413{{"`How to Call a Method`"}} end

Creating and calling an inbuilt method

To call an inbuilt method in Java, we can directly use the method name. In this step, we will call the Math.sqrt() method that returns the square root of a number.

  • Create a new Java file with the name CallMethod.java in the ~/project directory.

  • Add the following code in the file:

    public class CallMethod {
        public static void main(String[] args) {
            double num = 16;
            double ans = Math.sqrt(num); // calling the inbuilt method
            System.out.println("Square Root of "+num+" = "+ans); // printing the result
        }
    }
  • Save the file and close it.

  • To run the above code, open your terminal and navigate to the ~/project directory.

  • Compile the file using the javac command:

    javac CallMethod.java
  • Run the file using the java command:

    java CallMethod
  • After running the file you should see the output as follows:

    Square Root of 16.0 = 4.0

Creating and calling a user-defined method

To call a user-defined method in Java, we need to create an object of the class in which the method is defined. In this step, we will call a user-defined method hello().

  • Open the CallMethod.java file that we created in the previous step.

  • Add the following code below the main() method to define a user-defined method called hello():

    public void hello() {
        System.out.println("Hello World!");
    }
  • Modify the main() method to create an object of the CallMethod class and call the hello() method:

    public static void main(String[] args) {
        double num = 16;
        double ans = Math.sqrt(num);
        System.out.println("Square Root of "+num+" = "+ans);
        CallMethod obj = new CallMethod(); // creating object of CallMethod class
        obj.hello(); // calling user-defined method
    }
  • Save the file and close it.

  • To run the above code, open your terminal and navigate to the ~/project directory.

  • Compile the file using the javac command:

    javac CallMethod.java
  • Run the file using the java command:

    java CallMethod
  • After running the file you should see the output as follows:

    Square Root of 16.0 = 4.0
    Hello World!

Summary

In this lab, we have learned how to call both inbuilt and user-defined methods in Java. We can directly call inbuilt methods by their name, whereas to call user-defined methods, we need to create an object of the class in which the method is defined.

Other Java Tutorials you may like