• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Call Method in Java

July 31, 2023 //  by Talha Malik

In programming, the functions and methods serve as a prerequisite in implementing the required functionalities. These approaches are often used in programming by a noob programmer as well as a developer to create templates for the passed values. While defining these functionalities, it is also important to invoke/call these features to reflect the changes in the code.

How to Call Method in Java?

A method to be called in Java can be of various types, listed below:

  • Static Method.
  • Predefined Method.
  • User-defined Method.
  • Abstract Method

Note: A method can be overloaded and called which will be discussed later in the example.

Example 1: Calling a Static Method

This example calls a static method:

public class Methodcall {
public static void main(String args[]) {
  double x;
  x= Math.pow(2, 3);
  System.out.println("Value -> " + x);
}}

In this code, invoke a static method of the “Math” class to compute the power of 2 as 3 and return the outcome.

Output

Example 2: Calling a Predefined Method

This demonstration calls a predefined method of the “ArrayList” class:

import java.util.ArrayList;
public class Methodcall {
public static void main(String[] args) {
  ArrayList<Integer> items = new ArrayList<>();
  items.add(1);
  items.add(2);
  System.out.println("ArrayList Elements -> ");
  items.stream().forEach(System.out::println);
}}

In this code, call a predefined method “add()” of the ArrayList to insert the given integers into the created ArrayList and return the traversed array elements via the “for-Each” loop.

Output

Example 3: Calling a User-Defined Method

In this particular example, the user-defined “static” and “non-static” methods will be accessed:

public class Methodcall {
static void displayOut1() {
  System.out.println("This is a static user-defined function");
}
void displayOut2() {
  System.out.println("This is a non-static user-defined function");
}
public static void main(String[] args) {
  displayOut1();
  Methodcall x = new Methodcall();
  x.displayOut2();
}}

According to this snippet of code:

  • Define a user-defined static and non-static methods, respectively comprising the given messages.
  • In “main”, invoke the static method directly.
  • After that, create a class object to access the non-static method.

Output

Example 4: Calling an Abstract Method

This specific example calls an abstract method:

abstract class AbstractMethodExample{
abstract void display();
}
public class Methodcall extends AbstractMethodExample {
  void display() {
  System.out.println("Abstract Method Invoked!");
}
public static void main(String[] args){
  Methodcall x = new Methodcall();
  x.display();
}}

In this code:

  • Specify an abstract method in an abstract class.
  • Now, the public class extends this class and defines the specified abstract method with the given message statement.
  • In “main”, create an object of the main class and call the abstract method.

Output

Example 5: Overloading a Method

This code example overloads a method with the same name and then calls both the methods:

public class Methodcall {
static int add(int a, int b){
  return a + b;
}
static double add(double a, double b){
  return a + b;
}
public static void main(String[] args){
  System.out.println("Addition of Integers -> " +add(33, 45));
  System.out.println("Addition of Doubles -> " +add(2.32, 3.323));
}}

In the above code lines:

  • Define a method “add()” returning the addition of the passed integers.
  • Now, overload the defined method with the same name by returning the addition of the passed doubles instead.
  • In “main”, invoke both the functions with the passed integer and double values, respectively.

Output

Conclusion

A method in Java can be called directly, via an associated class etc. Calling a method depends on the type of method being invoked. This type can be static, predefined, user-defined, or abstract. Also, a method can be overloaded with the same name and invoked.

Category: Java

Previous Post: « What is the Purpose of “this” and “extends” in Java?
Next Post: How to Print ArrayList in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact