Dremendo Tag Line

Static Methods in Java Programming

OOPs in Java

In this lesson, we will understand what is Static Method in Java Programming and how to create them along with some examples.

What are Static Methods in Java?

Static Methods in Java are defined inside a class using the keyword static. These methods are bound to the class and not to the object, which means that these methods can be called using the class name, and calling the methods using the object name is not necessary. However, the object of the class can also access the static methods using the object name.

Static Methods are used to create a utility class, also known as the helper class, that contains only static methods and can be reused across the programs. It's not required to create an object of a utility class to access its static method because we can also access the static methods using its class name.



import java.util.Scanner;

class Calc
    public static float add(float a, float b)
        return a+b;

    public static float subtract(float a, float b)
        return a-b;

    public static float multiply(float a, float b)
        return a*b;

    public static float divide(float a, float b)
        return a/b;

public class Example
    public static void main(String args[])
        float x,y;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 numbers");

        // Call all the static methods of the class Calc one by one
        System.out.println("Addition = " + Calc.add(x,y));
        System.out.println("Subtraction = " + Calc.subtract(x,y));
        System.out.println("Multiplication = " + Calc.multiply(x,y));
        System.out.println("Division = " + Calc.divide(x,y));


Enter 2 numbers
Addition = 10.0
Subtraction = 6.0
Multiplication = 16.0
Division = 4.0