Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.