What is Static Method in Java [+Examples]?

Danielle Ellis
Danielle Ellis

Updated:

Published:

Being a part of something is, oftentimes, more beneficial than being on the outskirts. There are those who take center stage and those content with remaining in the background. For example, actors usually get all the attention, while crew members typically labor behind the scenes. Nevertheless, everyone has an important role to play.

Person presenting the meaning of static method

Download Now: An Introduction to Java & JavaScript

The same can be said of static and non-static methods in Java. Static methods are attached to a class, while non-static methods are not. In this article, we will discuss static methods, how to create them, and provide some examples to help illustrate their usage.

What is a Static Method in Java?

A static method is a method that belongs to a class rather than an instance of a class. This means you can call a static method without creating an object of the class. Static methods are sometimes called class methods.

How to Create Static Methods in Java

Static methods are created in Java using the static keyword. The syntax for creating a static method is as follows:

public static void methodName() { // method body }

Let's look at an example to see how this works. Say you have a class called CrewMember with two static methods, introduce and describe. The CrewMember class might look something like this:

public class CrewMember {     public static void introduce() {         System.out.println("Hi, my name is John.");     }     public static void describe() {         System.out.println("I am a production assistant.");     } }

In the code above, we have created two static methods, introduce and describe.

We can call these methods without creating an object of the CrewMember class. For example:

CrewMember.introduce(); // Hi, my name is John. CrewMember.describe(); // I am a production assistant.

As you can see, static methods are called using the class name followed by the method name with no need to create an object of the class.

Why Use Static Methods?

There are several reasons why you might want to use static methods. First, static methods can be called without creating an object of the class, which can be convenient if you only need to call the method once or if you don't need to store any data in an object after calling the method.

Second, static methods can be used to create utility classes, which are classes that contain only static methods. Utility classes are often used for mathematical operations, string manipulation, and file input/output.

Finally, static methods can be used to implement the singleton design pattern. A singleton is a class that can only have one instance. Singletons are often used for managing resources such as database connections or thread pools.

Static Method in Java Examples

Now that we've discussed what static methods are and why you might use them, let's look at some examples to see how they work in practice.

Example 1: Creating a Utility Class

Suppose we want to create a utility class that contains methods for performing mathematical operations. We could create a class called MathUtils with static methods for addition, subtraction, multiplication, and division. The MathUtils class might look like this:

public class MathUtils {     public static int add(int a, int b) {         return a + b;     }     public static int subtract(int a, int b) {         return a - b;     }     public static int multiply(int a, int b) {         return a * b;     }     public static int divide(int a, int b) {         return a / b;     } }

In the code above, we have created a utility class with four static methods for performing mathematical operations. We can call these methods without creating an object of the MathUtils class. For example:

int result = MathUtils.add(5, 7); // result is 12 result = MathUtils.subtract(10, 3); // result is 7 result = MathUtils.multiply(4, 2); // result is 8 result = MathUtils.divide(16, 4); // result is 4

Example 2: Implementing the Singleton Design Pattern

Singletons are often used for managing resources such as database connections or thread pools. Let's say we want to create a class that represents a database connection. We might want to ensure that there can only be one connection to the database at a time, so we can use the singleton design pattern.

The DatabaseConnection class might look like this:

public class DatabaseConnection {     // private constructor to prevent instantiation     private DatabaseConnection() {}     // static instance of the class     private static DatabaseConnection instance = null;     // method to get the instance of the class     public static DatabaseConnection getInstance() {         if (instance == null) {             instance = new DatabaseConnection();         }         return instance;     }     // other methods... }

In the code above, we have declared the constructor of the DatabaseConnection class to be private. This prevents other classes from instantiating theDatabaseConnection class. We also declare a static instance of the class and a static method for getting the instance of the class.

The getInstance() method first checks to see if an instance of the class has already been created. If not, it creates an instance of the class and returns it. Otherwise, it returns the existing instance. This ensures that there can only be one instance of the DatabaseConnection class at a time.

Getting Started With Static Methods

Static methods can be convenient and help you write more concise code. However, they should be used sparingly and only when it makes sense. In general, static methods are best used for utility classes and singletons. Otherwise, it is usually better to create an object of the class and call instance methods on that object. This gives you more flexibility and makes your code easier to maintain.

java

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.