Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Sitemap

Member-only story

Static vs. Non-Static Methods in Java (Java Interview QA)

Learn the core differences between static and non-static methods in Java with real examples. Understand when and how to use them in your applications.

2 min readApr 2, 2025

Introduction

In Java, you can define two types of methods:

  • 🔹 Static methods — belong to the class.
  • 🔸 Non-static methods — belong to an object (instance).

Let’s explore the differences with examples and when to use each.

Key Differences: Static vs Non-Static

1. Static Method Example

public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}

Usage:

int sum = MathUtils.add(10, 5); // No need to create an object

💡 Static methods are great for utility functions like Math.pow(), Arrays.sort().

2. Non-Static Method Example

public class Person {
private String name;

public Person(String name) {
this.name = name;
}

public void greet() {
System.out.println("Hi, my…

--

--

No responses yet