Java ProgramsOOPStatic Methods

Static Methods in Java

beginner·  OOP  ·  Classes & Objects

Problem

A static method belongs to the class itself, so it can be called through the class name directly — it never needs an object to exist first, unlike an instance method.

Write a utility method that squares a number, and call it without ever creating an object.

Input
square(6)
Output
Square of 6 is 36

Java Program

Java
class MathUtils { static int square(int n) { return n * n; } } public class StaticMethods { public static void main(String[] args) { int result = MathUtils.square(6); // called directly through the class, no object needed System.out.println("Square of 6 is " + result); } }

Output

Square of 6 is 36

Core Logic

Declaring square() as static lets it be called through the class name alone, since it doesn't need to read or change any particular object's fields.

How It Works
  1. 1static int square(int n) is declared directly on MathUtils, with the static keyword marking it as class-level rather than object-level.
  2. 2MathUtils.square(6) calls it directly through the class name — no new MathUtils() is ever needed.
  3. 3Because the method only works with the value passed in, it has no reason to depend on any object's state.
  4. 4The result is printed directly from the call's return value.
Calling MathUtils.square(6) returns 36, printed as "Square of 6 is 36".
💡

Key Point: An instance method would require an object before it could be called at all — the whole point of making square() static is that no such object is ever necessary.

Key Concepts

static methodclass-level methodutility method

Approach 2: Java 8

Java
import java.util.function.Function; public class StaticMethodReferenceDemo { static int square(int n) { return n * n; } public static void main(String[] args) { // Captures the static method itself as a value Function<Integer, Integer> squareFn = StaticMethodReferenceDemo::square; System.out.println("Square of 6 is " + squareFn.apply(6)); } }

Output

Square of 6 is 36

Core Logic

A static method can be referenced directly as a value using Java 8's method reference syntax, and stored in a functional interface variable instead of being called by name.

How It Works
  1. 1Function<Integer, Integer> squareFn = MathUtils::square; captures the static method itself as a value, using ClassName::methodName syntax.
  2. 2squareFn now behaves like any other Function — it can be passed around, stored, or invoked later.
  3. 3squareFn.apply(6) invokes the referenced static method with 6 as its argument.
  4. 4The result is identical to calling MathUtils.square(6) directly — the method reference is just another way to invoke the same static method.
MathUtils::square wraps the static method as a Function<Integer, Integer>, and calling .apply(6) on it returns 36, same as before.
💡

Key Point: Method references are most useful when a static method needs to be passed somewhere expecting a functional interface, like a Stream's map() — calling it directly by name, as the primary approach does, is simpler when no such interface is involved.

Key Concepts

method referenceFunction interfacefunctional interface

Related Programs