Table of contents
1.
Introduction
2.
Java Method calls
3.
Types of Methods
3.1.
User-defined Methods
3.1.1.
Example
3.2.
Abstract Methods
3.2.1.
Example
3.3.
Predefined Methods
3.3.1.
Example
3.4.
Static Methods
3.4.1.
Example
4.
Method Overloading
5.
Memory Allocation for Method Calls
6.
Frequently Asked Questions
6.1.
How to call a static method in Java?
6.2.
What is a super method call in Java?
6.3.
What is the difference between static and instance method calls in Java?
6.4.
Can a method call another method in Java?
7.
Conclusion
Last Updated: Oct 27, 2024
Medium

How to Call a Method in Java?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is one of the most popular languages among all the programming languages. It gives various advantages for using this language, including security purposes and large-scale building applications. One of the features of this language is that it connects with the concept of real-world problems using objects and classes.

how to call a method in java

In this article, we will discuss how to call a method in Java, covering different types of methods, including static and user-defined methods. We will learn their syntax, provide examples, and explain how method calls work in various contexts to enhance your understanding of Java programming.

Also read about, Duck Number in Java and Hashcode Method in Java.

Java Method calls

When you define a method in Java, you give it a return type that indicates what kind of value it will return. When you call the method, it processes the inputs and gives back a value based on what the user needs, helping to perform tasks effectively.

Types of Methods

We have four types of method calls:

1.User Defined Methods

2.Abstract Methods

3.Predefined Methods

4.Static Methods

User-defined Methods

The user creates this method based on his particular requirements.

Syntax

<ClassName> object = new <ClassName>
object.<MethodName>
You can also try this code with Online Java Compiler
Run Code

 

Example

public class Ex17 {


void hello()
{
	System.out.println("This is the userDefinedMethod");
	}
 	
	public static void main(String[] args)
	{
		Ex17 ob = new Ex17();
		ob.hello();
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

This is the userDefinedMethod
You can also try this code with Online Java Compiler
Run Code

In this code, we used one of the types of methods we have created, and after creating the object, we have called the user-defined function.

Abstract Methods

These are particular methods that do not require anybody to be implemented inside the abstract class.We must extend the abstract class in order to construct an instance of it.

Example

abstract class EX {

	abstract void check(String name);
}


public class Ex18 extends EX {


	public static void main(String[] args)
	{
		Ex18 ob = new Ex18();


		ob.check("Hello CN");
	}


	@Override void check(String name)
	{
		System.out.println(name);
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Hello CN
You can also try this code with Online Java Compiler
Run Code

In this example, we have used the abstract method to print some output. We have used the extend keyword to use the abstract method. We have created an object and an override function to call the abstract method.

Must Read: Java System Out Println

Predefined Methods

These are types of methods predefined in the Java library to perform some operations and inherited by every java class.

Example

public class Ex19 {


public static void main(String[] args) {
{
    // Creating object of the class in
    // main() method
    Ex19 ob = new Ex19();
 
    // Print the hashcode using
    // predefined hashCode() method
    System.out.println(ob.hashCode());
    }
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

476402209
You can also try this code with Online Java Compiler
Run Code

In this example, we have used a predefined function hashCode(). This function prints the hashcode of the object. 

Practice this code by yourself on Online Java Compiler.

Static Methods

These methods can be accessed without any class instance, i.e., you do not require any object to call that method.

Example

public class Ex20 {


    static void hello()
    {
        System.out.println("Hello");
    }
    public static void main(String[] args)
    {
       hello();
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output 

Hello

In this example, we have used a static method. This method does not require any object to call. It simply returns the value when it is being called.

Method Overloading

It’s possible to have multiple methods with the same name in the same class but different parameters (maybe different number of parameters or maybe different data types of parameters) and different functionalities. This is called method overloading.

When more than one method of the same name is created in a class, this method is called an overloaded method.

public class TypesOfMethod 
{
    public static void cube()
    {
        System.out.println("No parameter method is called");
    }
    public static void cube(int num)
    {
        System.out.println("Method with integer argument is called");
        int cube_value = num * num  *num;
        System.out.println(cube_value);
    }
 
    public static void cube(double num)
    {
        System.out.println("Method with double argument is called");
        double cube_value = num *num * num;
        System.out.println(cube_value);
 
    }
 
    public static void main(String[] args) 
    {
        cube();  // No parameter method called
        cube(7); // Integer parameter method called
        cube(6.0); // double parameter method called
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

No parameter method is called
Method with integer argument is called
343
The method with double argument is called
216.0

In the above example, we do not explicitly specify the integer method, float method, or no argument method. The Java compiler itself is capable of performing the appropriate method call for an object based on the data type of the argument/parameter passed to it. 

Memory Allocation for Method Calls

Whenever a java program is compiled, the JVM first looks for the main() method. Inside the main method, instructions are executed sequentially. There may be one or more than one method called inside the main method.

The Java Virtual Machine(JVM)  divides the memory into stacks and heaps. The stack memory may grow and shrink as and when required. Any variable defined in the stack lasts as long as the scope of the method exists. To keep track of the method calls, they are implemented using a Stack. 

Whenever a method is called, a new stack frame is created. Within the stack frame, all the variables and the arguments passed to it are stored. The stack frame would be deleted as and when the execution of the method is done.

Frequently Asked Questions

How to call a static method in Java?

If a function is written as static, the method is termed as static method. This function is called by using this format ClassName.methodName.

What is a super method call in Java?

A super method call in Java is used to invoke a method from a superclass within a subclass. This allows the subclass to access overridden methods in the parent class, enabling code reuse and maintaining the inheritance hierarchy.

What is the difference between static and instance method calls in Java?

Static method calls are made on the class itself and can be invoked without creating an instance. Instance method calls require an object of the class and operate on the specific instance's data, reflecting its state.

Can a method call another method in Java?

Yes, a method in Java can call another method, either within the same class or from other classes. This allows for modular code organization, enhancing readability and maintainability by breaking tasks into smaller, reusable methods.

Conclusion

In this blog, we have discussed Method calls in Java. We also covered what a method call is and the types of methods with their syntax. We took a different set of examples based on the different types of methods. We also discussed code and its output.

Recommended Readings:

If you want to learn more about this topic and practice more questions asked in renowned companies, feel free to jump to our website, Interview Problems.

Live masterclass