Sitemap

🧩 Static Members in Java — The Complete Guide (Easy to Advanced)

4 min readJul 15, 2025

--

✅ What You Will Learn

By the end of this blog, you’ll clearly understand:

  • What static means in Java
  • How static variables, methods, blocks, and classes work
  • Real-life examples with code
  • When and why to use static
  • Common mistakes and best practices

Even if you’re new to coding — this will make sense. Let’s start!

🔹 What is a Static Member in Java?

In Java, a static member belongs to the class, not the object.

Get Ravi Yadav’s stories in your inbox

Join Medium for free to get updates from this writer.

That means:

  • You don’t need to create an object to use it.
  • All objects share the same static variable.
  • Static methods can be called directly using the class name.

🔸 1. Static Variable (Shared Variable)

A static variable is shared by all objects of a class.

🔍 Example:

public class Student {
String name;
static String school = "ABC School";

Student(String name) {
this.name = name;
}

void show() {
System.out.println(name + " studies at " + school);
}
}

public class Main {
public static void main(String[] args) {
Student s1 = new Student("John");
Student s2 = new Student("Emily");

s1.show(); // John studies at ABC School
s2.show(); // Emily studies at ABC School

// Change static variable
Student.school = "XYZ School";

s1.show(); // John studies at XYZ School
s2.show(); // Emily studies at XYZ School
}
}

💡 Key Point:

All objects see the same value of school. Changing it once changes it for all.

🔸 2. Static Method (Used Without Object)

Static methods can be called without creating an object. They can only access static variables.

🔍 Example:

public class MathUtils {
public static int square(int n) {
return n * n;
}
}

public class Main {
public static void main(String[] args) {
int result = MathUtils.square(5);
System.out.println(result); // 25
}
}

❌ Wrong:

Static methods cannot use non-static variables directly, because they don’t belong to any object.

🔸 3. Static Block (Runs Once)

Static blocks are used to initialize static variables. They run once when the class is loaded.

🔍 Example:

public class Example {
static int number;

static {
number = 10;
System.out.println("Static block executed");
}

public static void show() {
System.out.println("Number: " + number);
}
}

public class Main {
public static void main(String[] args) {
Example.show();
}
}

⏱ Output:

Static block executed
Number: 10

🔸 4. Static Nested Class

Java allows you to create a class inside another class. If the nested class is static, it does not need an object of the outer class.

🔍 Example:

public class Outer {
static class Inner {
void display() {
System.out.println("Inside static nested class");
}
}
}

public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}

🧠 When Should You Use static in Java?

Use static when the behavior or data doesn't depend on a specific object. Here’s a simple guideline:

  • ✅ Use static if the value or method is the same for all instances of the class.
  • ✅ Use it for utility or helper functions, like Math.pow(), that don't rely on instance data.
  • ✅ Use it when something needs to be accessed without creating an object (e.g., ClassName.method()).
  • ❌ Avoid static if the behavior depends on instance-specific data — in that case, instance members are the right choice.

⚠️ Common Mistakes with static

Here are some common pitfalls to watch out for when using static members:

  • ❌ Trying to use this in a static method
    Static methods don’t belong to any specific object, so they can’t use this.
  • ❌ Accessing non-static (instance) variables from a static method
    This doesn’t work because static methods don’t have access to object-level data unless you explicitly pass an object reference.
  • ❌ Overusing static in your code
    While static can be powerful, using it everywhere can break object-oriented design principles, making your code harder to manage and extend.

🧪 Bonus: Static and Singleton

Static is useful in design patterns like Singleton, where only one object should be created.

public class Singleton {
private static Singleton instance;

private Singleton() {}

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

✅ Summary

Here’s a quick recap of the main static features in Java:

  • static variable – Shared by all objects of the class. Any change affects all instances.
  • static method – Can be called without creating an object. Ideal for utility logic.
  • static block – Executes once when the class is loaded. Used for static initialization.
  • static nested class – Can be used without an object of the outer class.

🏁 Final Thoughts

Always keep this in mind:

Static belongs to the class, not to any specific object.

Once that concept clicks, using static becomes much easier — and far more powerful. It helps you write cleaner, more efficient code when used correctly.

And remember:

You don’t need to work at a tech company to learn this —
You’ve just understood it better than many who already do.

--

--