BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to Call a Method in Java

By Chaitanya Singh | Filed Under: java

In this article, we will learn how to call a method in Java. A method is a set of instructions, created to perform a specific task. Instead of writing the same set of instructions again and again, we simply create a method in java that has those instructions inside the method body and this method can be called whenever we need to perform that specific task. This facilitates the reusability in our code as we don’t have to write same set of statements multiple times.

How to call a method in Java

Let’s take a simple example first to understand how to create a method and then call it. In the following example, we have created a simple method hello() that just prints a text message and we have called the method inside the main() method of the same class. As you can see we have to create the object of the class first and then we called the method using the object.

public class JavaExample {
    // A method
    public void hello() {
        System.out.println("Hey, I'm just a method.");
        System.out.println("Hello");
    }

    public static void main(String[] args) {
        //Creating the object of the class
        JavaExample obj = new JavaExample();
        //Calling the method
        obj.hello();
    }
}

Output:

Hey, I'm just a method.
Hello

Calling methods with multiple parameters

In the above example, we learned how to call a method with no arguments. In this section, we will learn how to call a method with parameters by passing arguments. In the following example, we have created a method with two int parameters, this means whenever we call this method, we have to pass two integer parameters.

public class JavaExample {
    public int add(int num1, int num2) {
        return num1 + num2;
    }

    public static void main(String[] args) {
        JavaExample obj = new JavaExample();
        //Calling the method
        int sum = obj.add(10,5);
        System.out.println("Sum: " + sum);
    }
}

Output:

Sum: 15

How to call a static method in Java

A static method in java can be called without creating the object of the class. Here we will learn how to call a static method in Java. To learn more about static method refer this guide: Static method in Java.

We can call a static method by using ClassName.MethodName. For example, class Math contains a static method max(int a, int b) that returns the largest number when two numbers are passed as an arguments to it. We can call this method by using Math.max(int a, int b).

Let’s have a look at the complete code:

import java.util.Scanner;
public class JavaExample
{
    public static void main(String args[])
    {
        int num1, num2, max;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First int number:");
        num1 = sc.nextInt();
        System.out.println("Enter Second int number:");
        num2 = sc.nextInt();
        sc.close();
        //Here we are calling the static method max of Math class
        max = Math.max(num1,num2);
        System.out.println("Max number between "+num1+" and "+ num2+" is: "+ max);
    }
}

Output:

Enter First int number:
22
Enter Second int number:
8
Max number between 22 and 8 is: 22

Process finished with exit code 0

Explanation: As you can see in the above example, we called a static method max() that belongs to the Math class, we didn’t need to create the object of the Math class and we simply used ClassName.MethodName to call the static method.

Notes:

  • We can create multiple methods with the same name but different number, sequence or type of arguments using method overloading.
  • We can also override the method in subclass which has already been declared in the parent class. This is known as method overriding and to learn this concept in details with examples refer this guide: Method overriding in Java.
❮ Java Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap