Hamburger Icon

What is static or non-Static in Java: Concepts and use cases

Feeling confused about what is static and what is non-static in Java? Read on to get clarity with key concepts and best use cases to boost your coding skills.

Apr 3, 2025 • 12 Minute Read

Please set an alt value for this image...
  • Software Development
  • Guides
  • Upskilling

Learning about static and non-static in Java can be a tough topic! To make it easier, let’s step away from code for a moment and talk about dogs (puppies make everybody happy, so you’ll be mentally prepared for the rest of this article).

Ready? Let’s talk puppies.

The dog analogy: Static vs. non-Static explained

So, we are building a Java application. Some parts of the program should work the same for everyone, all the time, no matter what specific “instance” (also called an object) you’re dealing with. Other parts, however, absolutely need an instance to do their job because they’re unique to that object and only make sense when they are about a specific object.

For example: You don’t need to own a dog to buy dog food (recommended though). Anyone can walk into a pet store and pick up a bag of dog kibble. This is a “static” scenario—no specific dog is needed.

But if you want to walk a dog or dye its fur pink, you absolutely need a real, living dog. This is a “non-static” scenario, where an actual instance must exist. Also, walking this dog will only walk one dog, and dyeing its fur pink (not necessarily recommended) will only dye that specific dog’s fur pink and not the fur of all dogs (can you imagine?!).

So, the distinction is about actions or properties that require an instance versus those that don’t. Let’s see how this logic carries over into programming.

What’s the difference between classes and objects in Java?

In order to understand static well, you need to grasp these two concepts: classes and objects (or instances, same thing).

A class is a blueprint for an object. Let’s move from puppies to houses. My house example is a nice way to understand classes (blueprints) and objects (actual instances). Houses in real life have blueprints as well, but here’s the Java version:

      public class House {
    // Static property: shared by all House objects
    public static int houseCount = 0;

    // Non-static (instance) properties: unique to each House 
object
    private int numberOfBedrooms;
    private String color;
    private boolean hasGarage;

    // Constructor
    public House(int numberOfBedrooms, String color, boolean 
hasGarage) {
        this.numberOfBedrooms = numberOfBedrooms;
        this.color = color;
        this.hasGarage = hasGarage;

        // Each time we create a new House object, increment the 
static count
        houseCount++;
    }

    // Omitted getters and setters
}
    

This is not an actual house yet. Much like having a blueprint of a house is not having a house yet, it’s just the design. In order to get a house, we need to build one. And to get a Java house, we need to create an object. An object is created with the constructor of a class, for example:

      House house1 = new House(3, "Pink", true);
House house2 = new House(4, "Red", false);
House house3 = new House(2, "Purple", true);
    

Here we have created three different house instances.

So, long story short, a class is a blueprint, and an object is an actual instance of this type. Now that you have the definition of class and object clear, let’s move on to explaining non-static and static further.

What is non-static?

The non-static members require an object because they’re specific to each object of that class. If you’re going to walk a dog, that dog must exist. If you’re going to paint a door of the house, that actual house must exist.

Methods and instances can be variable. If you have a class with instance variables specified, that means that each object has its own copy of these variables. One house has a pink door, the other house has a purple door. Or, the dog Bobby has a name, and Rex has a name, etc. In Java, the Dog class might have a name, age, breed, and a walk() method. The instances differ because each dog object is unique!

The instance methods are a bit different, but similar. Since we’re talking about methods, it’s about actions. In terms of objects, behaviors that belong to a particular object. Bobby might have a bark() method that’s different from Rex’s bark because the bark() method might rely on an instance variable.

How to make methods non-Static in Java

This is a question I often get from beginners in Java. Making methods non-static is easy. You just don’t use the word static in your method definition. In the below snippet, I created a static and a non-static method. The non-static method simply doesn’t have the keyword. Can you figure out which one it is?

      public class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    public void bark() {
        System.out.println(name + " says: Woof!");
    }

    public static void dogInfo() {
        System.out.println("Dogs are wonderful companions!");
    }
}

    

The static method is dogInfo() and the non-static method is bark(). This means that bark() belongs to a specific Dog object, so it needs an instance to work. In this case, Bobby’s bark will differ from Rex’s bark because each has a unique name, and we use that in the bark() method. So now that we’ve created a non-static method, let’s see how we can call it.

Calling the non-static method

Calling a non-static method can only be done directly from another non-static class. But we can call it in a static context as well (like main()), but then we need to create an instance first:

      public class Main {
    public static void main(String[] args) {
                
        // bark() requires an instance
        Dog bobby = new Dog("Bobby");
        bobby.bark();  // "Bobby says: Woof!"

        // dogInfo() can be called without an instance
        Dog.dogInfo();
    }
}

    

From another non-static method within the same class, you can call non-static methods directly. No need to create a new instance because you’re already “inside” an instance. Here’s an example:

      public class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    public void bark() {
        System.out.println(name + " says: Woof!");
    }

    // Another non-static method
    public void respondToNoise() {
        // Here we can just call bark() directly
        bark();
        System.out.println(name + " walks to its human.");
    }
}
    

And that’s how to create and call non-static methods. Let’s clarify the concept of non-static classes as well!

How to make a non-static class

Another common question: how to make a non-static class. Well, again, easy! Just don’t put the word static in front of it. When you are creating a top-level class, a class that’s not inside another class, it’s not even allowed to make it static. So a non-static class is something we’ve seen already.

Here’s an example of how to do it:

      public class House {
    private String color;

    public House(String color) {
        this.color = color;
    }

    public void paint(String newColor) {
        System.out.println("Painting the house " + newColor);
        this.color = newColor;
    }

    public String getColor() {
        return this.color;
    }
}
    

So yes, congratulations, you already have experience with non-static classes. If you ever see a static class in Java, it’s for nested classes (inner classes) declared inside another class. But for your everyday top-level classes, you don’t have to worry about making them non-static. They are!

And that’s it! Non-static means you’re dealing with real objects, each with its own data (a pink door, a purple door, Bobby the dog, Rex the dog). If an action or detail belongs specifically to one instance, it should be non-static. And if it belongs to the entire class (like a general statement about all dogs or a count of houses created based on a class), that’s where static might come in handy. Let’s talk about that more!

What is static?

In Java, static means that something belongs to the class itself rather than any individual object created from that class.

Let’s explain this using our house example. Suppose we have a House blueprint and create three different House objects. These houses have non-static properties, such as whether they have a garage. One house might have a garage, while another might not. One house could be red, and another pink. If we repaint the red house yellow, it doesn’t affect the pink house—these fields belong to specific instances of the class.

Now, let’s introduce a static property: the total number of houses built. This value exists even before any houses are created—initially, it's 0. However, before building any houses, we don’t have a color yet, because no house exists.

To summarize:

  • Static members exist even when no instances have been created, and they are not tied to any specific instance.
  • Non-static members only exist when an instance is created, and they belong to that specific instance.

Now, let’s explore where we can use static in Java.

What can be static in Java?

So far, we’ve seen static properties (the number of houses built) and discussed static methods (like walking a dog or buying dog supplies). In Java, different elements can be static:

  • Variables – A single shared variable for all instances of a class.
  • Methods – Methods you can call without creating an object.
  • Code blocks – Special blocks that run once when the class is first loaded. (If you’re new to Java, you may not have seen this yet. Even experienced developers might not use it often, so don’t worry if it feels unfamiliar.)
  • Nested classes – A class inside another class can be static, but a regular (top-level) class cannot.

A key rule to remember: A static method cannot access instance variables or methods.

Why? Because static methods belong to the class, not an instance, and there might not even be a single instance of the class when they are called. That’s why attempting to access an instance variable inside a static method results in a compile-time error.

From a static context (a static block, a static nested class, or a static method), you can only access other static elements and constructors. However, you can create new objects inside a static method, and through these objects, you can access their instance variables and methods.

How static affects memory allocation in Java

I won’t go in depth here, but static members are stored in a special area of memory. They’re allocated when the class is loaded, and they persist as long as the class is in use. This is why when you make a change to a static variable, it’s visible everywhere. They are only in one location. So a change means a change for all the rest of the application’s perspective.

Static elements in our code are very important and help a lot for structuring our applications. Let’s see some typical use cases.

Static use cases: Four examples

Use cases for static methods

Whenever we have a function that’s more like a utility function, it makes sense to make it static. Think of a function like Math.max(a, b). It would be weird if you’d have to create a Math instance first. That’s why they made it static, so you don’t need to create a “Math” object to use it. You just call Math.max(….). Here’s a custom example of such a utility class with a static method:

      public class TextUtils {
    public static String capitalize(String input) {
        return input.substring(0,1).toUpperCase() + input.substring(1).toLowerCase();
    }
}
    

Now you can call:

      String capitalized = TextUtils.capitalize("hello");  
System.out.println(capitalized);  // Output: Hello
    

Notice that we don’t need an instance of TextUtils to call capitalize(). This is exactly what static methods are for—functionality that doesn’t require an object. And of course, the most famous static method in Java: public static void main(String[] args).

The main method is static because the program must start before any objects exist. If main were non-static, Java would need an instance of its class before running, which isn’t possible at startup.

Another advanced use case of static methods is in the singleton pattern, where a class ensures there’s only one instance throughout the entire application.

      public class DatabaseConnection {
    private static DatabaseConnection instance;

    private DatabaseConnection() {
        // Private constructor prevents direct instantiation
    }

    public static DatabaseConnection getInstance() {
        if (instance == null) {
            instance = new DatabaseConnection();
        }
        return instance;
    }

    public void connect() {
        System.out.println("Connected to the database!");
    }
}
    

This method ensures that DatabaseConnection only has one instance in the entire program.

Use cases for static variables

Static variables are great for:

  • Storing values shared across all objects (e.g., houseCount in a House class).
  • Tracking class-level data (e.g., number of users in an application).

Example:

      public class House {
    public static int houseCount = 0;  // Shared by all houses

    private String color;

    public House(String color) {
        this.color = color;
        houseCount++;  // Increments when a new house is created
    }
}
    

Now, even if we create multiple houses:

      House h1 = new House("Red");
House h2 = new House("Blue");

System.out.println(House.houseCount);  // Output: 2
    

The value of houseCount is shared between all House instances.

Use cases for static code blocks

A static code block (static { ... }) runs once when the class is loaded. It’s great for:

  • Initializing static variables.
  • Loading configuration settings at startup.

Example:

      public class ConfigManager {
    private static String defaultConfig;

    static {
        System.out.println("Loading config...");
        defaultConfig = "DEFAULT_CONFIG_VALUE";
    }

    public static String getDefaultConfig() {
        return defaultConfig;
    }
}
    

Since this block runs once when the class is first used, it ensures configuration is set up before any method calls.

Creating static nested classes

Static nested classes are useful when:

  • You need a class inside another class, but it doesn’t need access to instance variables.
  • You want to logically group related functionality.

Example:

      public class OuterClass {
    private String message = "Hello from OuterClass!";

    // Static nested class
    public static class NestedHelper {
        public void doSomething() {
            System.out.println("I'm a static nested class!");
        }
    }
}
    

Since NestedHelper is static, we can use it without an instance of OuterClass:

      OuterClass.NestedHelper helper = new 
OuterClass.NestedHelper();
helper.doSomething();
    

This is different from a non-static inner class, which requires an instance of the outer class.

Key takeaways: Static vs. non-static in Java

Static elements in Java belong to the class itself rather than any specific object.
To sum up:

  • Use static when something is shared among all instances (e.g., utility methods, counters).
  • Use non-static when it’s unique to an object (e.g., a dog’s name, a house’s color).
  • Static members are stored in a special memory area and persist throughout the program.
  • Syntax differences:
    • Non-static: myDog.bark();
    • Static: Dog.printDogCount();

Now that you know the difference between static and non-static, you can confidently structure your Java classes!

Happy coding!

Maaike van Putten

Maaike v.

Maaike is a trainer and software developer. She founded training agency Brightboost in 2014 and spends most of her days and nights working and learning. Training gives her the opportunity to combine her love for software development with her passion to help others boost their careers and be successful. She has trained professionals in the field of Java, Spring, C#, Python, Scrum, React and Angular. A lot of her time is spend staying up-to-date with the latest developments in her field.

More about this author