---
title: "Types of Methods in Java"  
description: "Methods are used to define reusable blocks of code. Methods can be categorized into different types based on their definition, usage, and functionalit"  
author: "Ashutosh Patel"  
published: 2025-03-20  
updated: 2025-03-20  
canonical: https://www.mindstick.com/articles/338819/types-of-methods-in-java  
category: "java"  
tags: ["java", "methods"]  
reading_time: 5 minutes  

---

# Types of Methods in Java

In Java, methods are used to define reusable blocks of code. Methods can be classified into different types based on their **definition**, **usage**, and **functionality**.

## 1. Predefined (Built-in) Methods

These methods are already provided by the standard libraries of Java (JDK). They belong to various classes such as `Math`, `String`, `Arrays`, etc.

## Example

```java
public class PredefinedMethods {
   public static void main(String[] args) {
       int max = Math.max(10, 20);  // Returns the maximum number
       System.out.println("Maximum: " + max);
       String str = "Hello";
       System.out.println("Length: " + str.length());  // Returns string length
   }
}
```

## Some Common Built-in Methods

| **Method** | **Description** |
| --- | --- |
| `Math.max(a, b)` | Returns the maximum of two numbers `a`, and `b` |
| `Math.sqrt(n)` | Returns the square root of `n` |
| `String.length()` | Returns the [length of a string](https://answers.mindstick.com/qa/104780/how-to-find-the-length-of-a-string) |
| `Arrays.sort(array)` | Sorts an array |

## 2. User-Defined Methods

These are methods created by programmers to perform specific tasks. They improve the reusability and modularity of the code.

## Example

```java
public class UserDefinedMethod {
   static void greet() { // Method definition
       System.out.println("Hello, Welcome to Java!");
   }
   public static void main(String[] args) {
       greet(); // Method call
   }
}
```

## 3. Static Methods

A method declared with the `static` keyword belongs to a class rather than an **instance**. It can also be called without creating an object.

## Example

```java
 class StaticExample {
   static void display() {
       System.out.println("This is a static method.");
   }
   public static void main(String[] args) {
       display();  // No need to create an object
   }
}
```

## Key Points

- Can be called using `ClassName.methodName()`.
- Non-static (instance) variables or methods cannot be used directly.
- Usually used for utility functions.

## 4. Instance Methods

These methods work on the [instance variables](https://www.mindstick.com/forum/34631/instance-variables-and-class-variables) of a class. They **require an objec**t to be invoked.

## Example

```java
class InstanceExample {
   void show() { // Instance method
       System.out.println("This is an instance method.");
   }
   public static void main(String[] args) {
       InstanceExample obj = new InstanceExample();
       obj.show(); // Calling instance method using object
   }
}
```

## Key Points

- Requires an object of the class to be called.
- Can access instance variables and other instance methods.

## 5. Parameterized Methods

These methods takes parameters (arguments) to perform operations based on the input values.

## Example

```java
public class ParameterizedMethod {
   static void sum(int a, int b) {  // Method with parameters
       System.out.println("Sum: " + (a + b));
   }
   public static void main(String[] args) {
       sum(5, 10);  // Passing arguments
   }
}
```

## Key Points

- Parameters allow methods to work dynamically.
- [Multiple parameters](https://www.mindstick.com/forum/33717/methods-multiple-parameters-structure) can be used

**6. [Method Overloading](https://www.mindstick.com/forum/156845/what-is-method-overloading-in-c-sharp-give-an-example)**

This allows multiple methods in the same class to have the same name but different parameters (different numbers or types of arguments).

## Example

```java
class OverloadingExample {
   static void show(int a) {
       System.out.println("Integer: " + a);
   }
   static void show(String text) {
       System.out.println("String: " + text);
   }
   public static void main(String[] args) {
       show(100);
       show("Hello");
   }
}
```

## Key Points

- Same method name, different parameter lists.
- The compiler decides which method to call based on the arguments.

## 7. [Method Overriding](https://www.mindstick.com/forum/53/method-overriding)

If a subclass defines a method with the **same name, return type,** and **parameters** as a method of the parent class, then it overrides the parent method.

## Example-

```java
public class OverrideExample {
   public static void main(String[] args) {
       Child obj = new Child();
       obj.display(); // Calls the overridden method in Child class
   }
}
class Parent {
   void display() {
       System.out.println("Parent class method.");
   }
}
class Child extends Parent {
   @Override
   void display() {
       System.out.println("Child class method.");
   }
}
```

## Key Points

- Used in inheritance to modify the behaviour of a method in a subclass.
- The `@Override` annotation ensures correct overriding.

## 8. Recursive Methods

A method that calls itself is called a recursive method. It is usually used for problems like factorial calculation, [Fibonacci series](https://www.mindstick.com/forum/156844/write-a-c-sharp-program-to-print-the-fibonacci-series), etc.

## Example-

```java
class RecursionExample {
   static int factorial(int n) {
       if (n == 1) return 1;  // Base case
       return n * factorial(n - 1);  // Recursive call
   }
   public static void main(String[] args) {
       System.out.println("Factorial of 5: " + factorial(5)); // Output: 120
   }
}
```

## Main Points

- Every [recursive function](https://www.mindstick.com/forum/156808/write-a-program-for-making-recursive-function-in-any-language) must have a **base case** to prevent infinite recursion.
- Used to solve problems that can be broken down into smaller sub-problems.

## 9. Abstract Methods

An **abstract method** is declared in an **abstract class** and must be implemented by subclasses. It has no body in an abstract class.

## Example-

```java
public class AbstractExample {
   public static void main(String[] args) {
       Dog d = new Dog();
       d.makeSound(); // Output: Dog barks.
   }
}
abstract class Animal {
   abstract void makeSound(); // Abstract method (no body)
}
class Dog extends Animal {
   void makeSound() {  // Implementing the abstract method
       System.out.println("Dog barks.");
   }
}
```

## Key Points

- Abstract methods must be implemented by subclasses.
- Used to enforce a common structure across subclasses.

## 10. Final Methods

A method declared as `final` cannot be overridden by subclasses.

```java
class Parent {
   final void show() {
       System.out.println("Final method in Parent class.");
   }
}
class Child extends Parent {
   void show(){
       System.out.println("Child method in Parent class.");
   }// Cannot override show() method because it is final

   public static void main(String[] arg)
   {
       Child child = new Child();
       child.show();
   }
}
```

## Key Points

- Used when the behaviour of a method should not be changed in subclasses.
- Improves security and prevents unintended modifications.

## Summary

Methods in Java improve [code reusability](https://answers.mindstick.com/qa/113734/how-does-object-oriented-programming-enhance-code-reusability-and-maintainability) and organization. By understanding the different types of methods, you can write efficient and [maintainable code](https://answers.mindstick.com/qa/111596/what-are-the-best-practices-for-writing-clean-and-maintainable-code) in Java.

Also, read: [Pass by Value vs. Pass by Reference in Java](https://www.mindstick.com/articles/338817/pass-by-value-vs-pass-by-reference-in-java)

---

Original Source: https://www.mindstick.com/articles/338819/types-of-methods-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
