Static Method

Reviewed by: Juliano Rabelo
Last Updated: August 14, 2020

Definition - What does Static Method mean?

In Java, a static method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that member of a class.

A static method is not part of the objects it creates but is part of a class definition. Unlike instance methods, a static method is referenced by the class name and can be invoked without creating an object of class.

In simpler terms, they are methods that exist even if no object has been constructed yet and that do not require an invocation object.

Techopedia explains Static Method

Java allows developers to define static methods, which are available to every instance of a class.

In an instance of a class, the methods are able to access both variables in an instance and those belonging to a class, while static methods cannot access instance variables or methods directly.

Instead, static methods have to use object reference.

Common Use for Static Methods

The most common use for static methods is to access static variables. They are accessed by the class name and a dot (.) followed by the name of a method. They are declared with the keyword "static" when defining a method.

Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members, and since it operates on arguments it is usually able to accept them, perform calculation and return value.

Static Methods Are Often Utility Methods

Static methods are often utility methods found in System, Wrapper and Collections classes that are used because they’re quicker and more efficient.

They can be employed by different classes without having to create an instance. Methods are created as static when object state has no effect on their behavior because they depend only on their own parameters.

Java 8 Interfaces

Static methods can also be defined in interfaces in Java 8 and subsequent versions. To prevent errors, implementation classes can’t override interface static methods.

Static methods are usually preferred when:

  • All instance methods should share a specific piece of code.

  • You want to call method without having to create an instance of that class.

  • You must make sure that the utility class is never changed.

  • You don’t want that method’s definition to be overridden.

  • Your method is not using any instance variable, and the code does not depend on instance creation.

This definition was written in the context of Java
Share this: