Java
Object-Oriented Programming
Static Methods
Instance Methods
Method Overloading

Static and Instance methods with the same name?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Java, static and instance methods behave differently, but that does not mean they can freely share the same signature inside one class. The important rule is that Java method signatures are based on the method name and parameter types, not on whether the method is static.

The Short Answer

You cannot declare a static method and an instance method with the exact same name and parameter list in the same class. Java treats that as a duplicate method declaration.

This code does not compile:

java
1public class Example {
2    public static void printValue() {
3        System.out.println("static");
4    }
5
6    public void printValue() {
7        System.out.println("instance");
8    }
9}

The compiler rejects it because both methods have the same signature from Java’s point of view.

Why Java Rejects It

Method overloading in Java depends on parameter lists. Return type alone is not enough, and the static modifier is not part of the signature.

So these two declarations collide:

  • 'printValue() static'
  • 'printValue() instance'

The language does not keep them separate just because one belongs to the class and the other belongs to an object instance.

What You Can Do Instead

You can use the same method name if the parameter list is different:

java
1public class Example {
2    public static void printValue(String label) {
3        System.out.println("static: " + label);
4    }
5
6    public void printValue() {
7        System.out.println("instance");
8    }
9}

Now the methods are overloaded because their parameter lists differ. That is legal.

You can also choose different names if the semantics are different enough that overloading would be confusing. In practice, clear naming is often better than forcing the same name across static and instance behavior.

Static Hiding and Instance Overriding

Confusion often comes from inheritance rules. Static methods are hidden, while instance methods are overridden.

Example:

java
1class Parent {
2    public static void greet() {
3        System.out.println("parent static");
4    }
5
6    public void speak() {
7        System.out.println("parent instance");
8    }
9}
10
11class Child extends Parent {
12    public static void greet() {
13        System.out.println("child static");
14    }
15
16    @Override
17    public void speak() {
18        System.out.println("child instance");
19    }
20}

And usage:

java
Parent ref = new Child();
Parent.greet();
ref.speak();

greet() is resolved by the reference type used for the static call. speak() is resolved at runtime based on the actual object instance.

That difference sometimes makes developers think static and instance methods are more interchangeable than they really are. They are not.

Design Guidance

If a method needs object state, make it an instance method. If it does not depend on instance state and conceptually belongs to the class as a utility or factory, make it static.

Mixing both styles under the same name can make APIs harder to read even when the code is technically legal through overloading. Call sites should make it obvious whether they are acting on an object or using a class-level helper.

For example, a static factory named fromString and an instance method named toString communicate intent better than two overloaded methods that force the reader to inspect argument lists carefully.

Common Pitfalls

The biggest mistake is assuming static changes the Java method signature. It does not.

Another issue is trying to use return type differences to distinguish static and instance methods. Java will still reject that because return type is not enough for overloading.

Developers also confuse hiding with overriding. A static method in a subclass does not participate in polymorphism the way an instance method does.

Finally, even legal overloads can become poor API design if the same name is used for conceptually unrelated static and instance behavior.

Summary

  • Java does not allow a static and an instance method with the same name and parameter list in one class.
  • Method signatures are based on name and parameter types, not the static modifier.
  • Overloading works only when the parameter list changes.
  • Static methods are hidden in inheritance, while instance methods are overridden.
  • Choose method names based on clarity, not just on what the compiler allows.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.