01 / 20

Static vs. Instance
Methods

Learn to distinguish between methods that belong to the class and methods that belong to an object: when to use each, how they differ, and why it matters.

Java

What Is a Method?

A method is a named block of code inside a class that performs a specific task. In Java, every method lives inside a class, but not every method behaves the same way.

static method
Belongs to the class itself. Called without creating an object.
instance method
Belongs to an object. Requires an instance of the class to call.

The static Keyword

When you see the keyword static in a method signature, it means the method is associated with the class, not with any particular object created from it.

public class MathHelper { // static method: no object needed public static int add(int a, int b) { return a + b; } }

Instance Methods Need an Object

An instance method operates on the data stored inside a specific object. You must create an instance with new before you can call it.

public class Dog { private String name; public Dog(String name) { this.name = name; } // instance method: needs an object public String bark() { return name + " says Woof!"; } }

The Core Rule

MyClass static methods Belong to the class No access to instance fields Called via ClassName.method() Math.sqrt() Integer.parseInt() instance methods Belong to an object Can access instance fields Called via objectName.method() str.length() list.add()

What static Methods Can and Cannot Do

✓
Can access static fields and call other static methods in the same class.
✓
Can accept parameters and return values just like any method.
✕
Cannot use this: there is no object to refer to.
✕
Cannot access instance fields or call instance methods directly.

static vs. instance: Side by Side

static instance
Keyword static in signature No static keyword
Called on The class An object
Uses this? No Yes
Instance fields? Cannot access Can access
Typical use Utility/helper Object behavior
Example Math.max(a, b) str.toUpperCase()

How to Decide: Step by Step

1
Ask: Does this method need object-specific data? If it reads or writes an instance field, it must be an instance method.
2
Ask: Does the behavior vary per object? If every object would do the same thing with the same inputs, it can be static.
3
Check the call syntax. If you see ClassName.method(), it is static. If you see variableName.method(), it is instance.
4
Look for the keyword. A static keyword in the method header is the definitive clue.

Example 1: Identifying Method Types

public class Circle { private double radius; public Circle(double r) { this.radius = r; } // Which type is this? public double getArea() { return Math.PI * radius * radius; } // Which type is this? public static double areaFromRadius(double r) { return Math.PI * r * r; } }
getArea()
Instance. It reads this.radius: each Circle object has its own radius.
areaFromRadius(r)
Static. It takes radius as a parameter. No object data needed.

Example 2: Calling Each Type

// Calling a STATIC method double area1 = Circle.areaFromRadius(5.0); // Calling an INSTANCE method Circle c = new Circle(5.0); double area2 = c.getArea();

Notice: Circle.areaFromRadius() uses the class name. c.getArea() uses the variable name that holds the object.

Example 3: main Is Always Static

public class App { private String greeting = "Hello"; public static void main(String[] args) { // System.out.println(greeting); // ERROR! App app = new App(); System.out.println(app.greeting); // OK } }

Decision Flowchart

New method needed Needs object data? Yes instance method No Utility / helper function? Yes static method No Reconsider

Real-World Java API Examples

Static
Math.abs(-7)
Integer.parseInt("42")
Arrays.sort(arr)
Collections.max(list)
Instance
str.length()
list.add("item")
scanner.nextInt()
obj.toString()

Under the Hood: Memory

Class (shared) static int count = 0 static getCount() One copy in memory Shared by all objects Heap (per object) dog1 name="Rex" bark() dog2 name="Max" bark() Each object has its own copy of instance data

Common Mistakes

✕ Accessing instance fields from static
Writing this.name inside a static method. There is no this in a static context.
✕ Calling on the wrong target
Writing Math m = new Math(); to call Math.sqrt(). Math has only static methods and a private constructor.
✕ Forgetting to create an object
Writing Dog.bark() when bark() is an instance method. You need a Dog object first.

Question 1 of 5

Which keyword in a method signature tells you the method belongs to the class rather than to an object?

Question 2 of 5

Which statement about increment() and reset(Counter c) is correct?

Question 3 of 5

What happens if you try to access an instance variable directly inside a static method?

Question 4 of 5

Given String s = "hello"; which call is an instance method call?

Question 5 of 5

Why is Java's main method declared static?