Static Method

Why Trust Techopedia

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 object of a class.

Advertisements

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 also available to every instance of a class.

In an instance of a class, static methods cannot access variables in an instance and those belonging to a class. They can only access static fields and 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. It is usually used to operate on input arguments (which can always accept), 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 can be more efficient. They can be employed by different classes without having to create an instance, which can sometimes make the difference. 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 (although you could still have an instance method for that).

  • 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.

Advertisements

Related Terms

Margaret Rouse
Technology expert
Margaret Rouse
Technology expert

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.