Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll explore the difference between class methods and instance methods in Java.

In object-oriented programming, a method is the equivalent of a function. This means it’s an action that an object can perform. We use instance methods when they operate on member variables and use static methods when no instance of the class is required for the method to execute. Let’s understand this in more detail next.

2. Instance vs Static Methods

Like in most object-oriented languages, we create class definitions in Java and instantiate them as objects.

These objects have attributes associated with them (member variables) and methods that usually refer to these member variables. When methods reference non-static member variables, we must define them as instance methods.

We sometimes define a method that doesn’t reference member variables or only references static variables. When we do this, we can make the method a static method. This means that we don’t need an instance of the class to invoke the method.

There are differences in behavior between class and instance methods, so let’s get started with an example.

To define a method as static we simply need to use the static keyword. Here’s an example of a class that contains both a static method and an instance method:

public class MyClass {
  public static boolean isAllLowerCaseStatic(String word) {
    return word.toLowerCase().equals(word);
  }
  public boolean isAllLowerCaseInstance(String word) {
    return word.toLowerCase().equals(word);
  }
}

There’s an important difference to remember when we invoke these methods. To use an instance method, we must first instantiate the class that holds the method definition:

MyClass obj = new MyClass();
obj.isAllLowerCaseInstance("test");

In the case of invoking static methods, we can reference the class directly:

MyClass.isAllLowerCaseStatic("test");

Our isAllLowerCaseStatic() method is a good usage of a static method since it doesn’t reference any member variables belonging to the object instance.

It’s important to remember that while static methods may seem like a good choice, they can be difficult to unit test since there’s no object to mock.

Static methods can introduce concurrency issues if the method operates on a static member variable. In this case, we can use the synchronized keyword in the method definition.

3. Conclusion

In this article, we learned the difference between class or static methods and instance methods in Java. We discussed how to define static and instance methods and how to invoke each of them.

We should remember that the key difference is that we must invoke an instance method through an instantiated object, while we can access a static method directly through the class.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW