Open In App

How to Call Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Java Methods are the collection of statements used for performing certain tasks and for returning the end result to the user.Java Methods enables the reuse the code without retyping the code. Every class in Java has its own methods, either inherited methods or user-defined methods that are used to define the behavior of the class. In this article, we will discuss different types of methods in Java and how to call methods in Java.

Types of Methods in Java

There are mainly 4 methods in Java as mentioned below:

  1. User-Defined Methods: These are the methods implemented by the user in a particular class to perform a particular operation.
  2. Abstract Methods: These are the methods that do not contain the body of the method and implements inside the abstract class.
  3. Predefined Methods: These are the methods that are predefined and available in the Java library to perform operations, for eg the hashcode() method.
  4. static Methods: These are methods that are accessible without any instance of the class. The memory management of these methods is different from ordinary methods.

1. User-Defined Methods

User-Defined nonstatic methods can be called or accessed only with the help of an instance of the class.

Declaring a User-Defined Method

<ClassName> object=new <ClassName>

Calling a User-Defined Method

object.<MethodName>

Example 

Java
// Java Program to Illustrate User-Defined Methods

// Importing essential input output classes
import java.io.*;

// Class 1
class GFG {

    // Method 1
    // Method of this class
    void hello()
    {
        // Print statement whenever this method s called
        System.out.println("This is the userDefinedMethod");
    }

    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating instance of the class
        // inside the main() method
        GFG ob = new GFG();

        // Calling the method of class 1
        // inside class 2
        ob.hello();
    }
}

Output
This is the userDefinedMethod

2. Abstract Methods

These are the methods that are declared inside the abstract class without the implementation of the method of a particular signature or without signature. We cannot call it abstract methods. To create the instance of the abstract class, we have to extend the abstract class. Abstract Methods are used when we have to use the one-way property of the method in different ways.

Declaring the Abstract Methods

abstract class abc{
abstract void method(){
.....
}
}

Calling the Abstract Methods

// Abstract class is initialised an object
// which calls an abstract method
object.method()

Example

Java
// Java Program to Illustrate Abstract Methods

// Class 1
// Helper class acting as Abstract class
abstract class GFGhelp {

    // Creating abstract method
    abstract void check(String name);
}

// Class 2
// Main class extending to helper class
public class GFG extends GFGhelp {

    // main driver method
    public static void main(String[] args)
    {
        // Creating the instance of the class
        GFG ob = new GFG();

        // Accessing the abstract method
        ob.check("GFG");
    }

    // Extends the abstract method
    @Override void check(String name)
    {
        System.out.println(name);
    }
}

Output
GFG

3. Predefined Methods

These are the methods that are already implemented in the Java library or predefined and inherited by every Java class. For example, consider every class in the Java inherited object class that has various methods. hashcode() is one of the methods of the object class that is inherited by every class in Java.

Calling the Predefined Methods

// calling predefined method with an object
abc.method()

Example 

Java
// Java Program to Illustrate Predefined Methods

// Main class
public class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of the class in
        // main() method
        GFG ob = new GFG();

        // Print the hashcode using
        // predefined hashCode() method
        System.out.println(ob.hashCode());
    }
}

Output
1023892928

4. Static methods

Static methods are those methods that there is no need for an instance of the class to access. Basically, these methods are the class methods, and every static method is shared among all the instances equally.

Declaring of static methods

class abc{
static void method(){
.....
}
}

Calling of static methods

// calling function without initialising
// object
abc.method();

Example 

Java
// Java Program to Illustrate Static Methods

// Importing input  output classes
import java.io.*;

// Test class
class test {
    // Static method
    static void hello()
    {
        // Print statement
        System.out.println("Hello");
    }
}

// Main class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // calling the Method 1
        // Accessing method
        test.hello();
    }
}

Output
Hello


Last Updated : 28 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads