---
# System prepended metadata

title: how to call a method in java

---

# how to call a method in java
## Overview
A method in Java is a group of statements that carry out a certain action or function. It is commonly used because it enables code reuse, which refers to the ability to write code once and use it multiple times. It also allows for simple modification. Each method has a unique name that it goes by when called. The method is called and completes the stated task when the compiler reads the method name. We will learn how to call static, abstract, user-defined, and pre-defined methods in Java in this section.

## Scope
In this article we are going to learn about
* What is Method?
* What are types of methods?
* how to call a static method in java?
* how to call a predefined method in java?
* how to call user defined method in java
* how to call abstract method in java?
## What is a Method?
A method in Java is a group of statements that carry out a certain action or function. It is commonly used because it enables code reuse, which refers to the ability to write code once and use it multiple times. It also allows for simple modification. Each method has a unique name that it goes by when called. The method is called and completes the stated task when the compiler reads the method name. We will learn how to call static, abstract, user-defined, and pre-defined methods in Java in this section.
## Types of Methods 
Syntax of method are as 
```java=
returnType methodName() {
  // method body
}
```
There are two types of methods in Java:
### Predefined Method
Predefined methods in Java refer to the methods that are already defined in the Java class libraries. It is also known as the standard library method or built-in technique. These methods can be used immediately by simply calling them within the application at any point. Among the pre-defined methods are sqrt(), compareTo(), length(), and equals(). When we invoke any of the predefined methods in our programme, a sequence of background codes relevant to that method that are already present in the library run.
Within a class, each and every predefined method is declared. The java.io.PrintStream class defines methods like print(). The statement that we enter inside the method is printed. As an illustration, print("Java") prints Java to the console.

In this example we are going to use a predefined method in java which is `max()` which is defined in `Math` class
```java=
public class predefined   
{  
public static void main(String[] args)   
{  
// using the max() method of Math class  
System.out.print("The maximum number is: " + Math.max(10,20));  
}  
} 
```
**Output**
```java=
The maximum number is: 9
```
**Code Explanation**
The three predefined methods main(), print(), and max were utilised in the example above. Because they are predefined, we have used these methods directly without declaring them. The PrintStream class's print() method prints the outcome to the console. The Math class has a method called max() that returns the larger of two values.
### User-defined Method
A user-defined method is one that has been written by a user or programmer. These techniques are altered based on the situation.
Syntax:
```java=
returnType methodName() {
  // method body
}
```
**returnType** identifies the kind of value that a method will return. For instance, a method returns an integer value if the return type is int.The method's return type is void if it doesn't produce a value.

**methodName** is an identifier that is used to refer to a certain programme method.

**Method body**:The statements used in the method body are utilised to carry out some operations. The curly braces  enclose the method body in them.


## Calling Static Method in Java 
A static method in Java is one that is used without first constructing an object of the class in which it is defined. Static methods are any methods with the keyword static before the method name. By using the static keyword before the method name, we can also construct a static method. By using ClassName.methodName, we can invoke a static method.
The best example of the static method is the main() method. It is called without creating the object.
In this example we are going to call a method of type static in java
**Code**
```java=
import java.util.*;  
public class ascii
{  
public static void main(String args[])   
{  
	Scanner s=new Scanner(System.in);
	int a=s.nextInt();
    int b=s.nextInt();
//calling static method of the Math class  
a=Math.min(a,b);  
System.out.println("Minimum number is: " + a);  
}  
}  
```
**Output**
```java=
Enter two numbers
12 13
Minimum number is: 12
```
**Code Explanation**
The static function min() of the Math class, which returns the lower of two values, is called in the programme above.
## Calling the Pre-Defined Method in Java
Predefined methods in Java refer to the methods that are already defined in the Java class libraries. It is also known as the standard library method or built-in technique. These methods can be used immediately by simply calling them within the application at any point. Among the pre-defined methods are sqrt(), compareTo(), length(), and equals(). When we invoke any of the predefined methods in our programme, a sequence of background codes relevant to that method that are already present in the library run.
Within a class, each and every predefined method is declared. The `java.io.PrintStream` class defines methods like print(). The statement that we enter inside the method is printed. As an illustration, print("Java") prints Java to the console.

In this example we are going to use Predefined method called `hashCode()` which is created in Object class.

**Code**
```java=
public class Predefined
{  
public static void main(String[] args)   
{  
int a;      
Object obj=new Object();      
a=obj.hashCode();  
System.out.println("Hash Code of the object is: "+a);  
}  
}  
```
**Output**
```java=
Hash Code of the object is: 2036368507
```
**Code Explanation**
We utilised the hashCode() function of the Object class in the example above. We have generated an object obj of the Object class in order to call the predefined function hashCode() since it is a method of the Object class. We have used the object to execute the hashCode() method, which returns the object's hash code value.

## Calling User-Defined Method in Java
A user-defined method is one that has been written by a user or programmer. These techniques are altered based on the situation.

We must first create a method before using it in order to invoke a user-defined method. The method name, followed by parenthesis, must be entered when creating a method in the class (). A method header and method body make up a method definition.

We can call a method by using the following:

**`non static method example`**
`method_name();`

**`static method`**
If the method is a static method, we use the following:
```java=
obj.method_name();   //static method calling  
```
In this example we are going to call method in java of type static and non-static user-defined.

```java=
public class MethodCallExample  
{  
//user-defined static method  
static void showMessage()   
{  
System.out.println("The static method invoked.");  
}  
//user-defined non-static method  
void displayMessage()   
{  
System.out.println("Non-static method invoked.");  
}  
public static void main(String[] args)   
{  
//calling static method without using the object  
showMessage(); //called method  
//creating an object of the class  
MethodCallExample me=new MethodCallExample();  
//calling non-static method  
me.displayMessage(); //called method  
}  
}  
```
**Output**
```java=
The static method invoked.
Non-static method invoked.
```
**Code Explanation**
The two user-defined methods in the example above are showMessage() and displayMessage (). DisplayMessage() is a non-static method, whereas the showMessage() function is static.

Keep in mind that we didn't use the object; instead, we called the showMessage() method directly. While utilising the class object to invoke the displayMessage() method.
## Calling Abstract Method in Java
Any method with the abstract keyword in its declaration is an abstract method. There is simply a method declaration in the abstract method. The body of the abstract method defined in the other class. The abstract class must contain a declaration for the abstract method. We have the option of making abstract methods' visibility private or public. Because it goes against the benefit of the abstract method, we cannot declare an abstract method to be final.

The child's class must include the actual implementation of the abstract method. An abstract method can be described as follows:
```java=
abstract public void findArea();  
```

In this example we are going to call a Abstract method in java
**Code**
```java=
abstract class AbstractMethodExample   
{    
//abstract method declaration  
abstract void show();    
}    
public class AbstractMethodCalling extends AbstractMethodExample  
{    
//abstract method implementation      
void show()  
{  
System.out.println("The abstract method called.");  
}    
public static void main(String args[])  
{    
AbstractMethodExample obj = new AbstractMethodCalling();    
//calling abstract method   
obj.show();    
}    
}    
```
**Output**
```java=
The abstract method called.
```
**Code Explanation**
In the aforementioned example, a class called AbstractMethodExample was created and declared to be abstract. We have declared an abstract method called display in this class (). Then, we produced a new class called AbstractMethodCalling that extends AbstractMethodExample. We have the abstract method implemented in this class.

## Conclusion 

* A method in Java is a group of statements that carry out a certain action or function.
* A method in Java is a group of statements that carry out a certain action or function. It is commonly used because it enables code reuse, which refers to the ability to write code once and use it multiple times.
* There are two types of methods userdefined and predefined method.
* A static method in Java is one that is used without first constructing an object of the class in which it is defined.
* In this article we have learned what are methods and how to call method in java.