Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Method calls
3.
Types of Methods
4.
User-defined Methods
4.1.
Example
5.
Abstract Methods
5.1.
Example
6.
Predefined Methods
6.1.
Example
7.
Static Methods
7.1.
Example
8.
Method Overloading
9.
Memory Allocation for Method Calls
10.
Frequently Asked Questions
10.1.
How to call a static method in Java? 
10.2.
How do you call a user-defined method in Java? 
11.
Key Takeaways
Last Updated: Mar 27, 2024

Method Calls in Java

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.

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

Method calls

When a method is defined, it comes with a return type and calls it. Then here comes the part of a method call that returns some value based on the user's requirement.

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
    }
}

This will produce the following result:

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.
 

How do you call a user-defined method in Java?
 


To call a user-defined method, we first need to create a method and then call it. This method is called been created inside the main function. A method definition must contain these two things: method header and method body.


Recommended Readings:

Key Takeaways

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.

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