DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on

Static vs Non-Static in Java

What Does static Mean?

A static member belongs to the class, not to any specific object.

This means you can use it without creating an object of the class.

Example

class Student {
    static String school = "ABC School";
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Student.school);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, school is static. Since it belongs to the class, I can access it using the class name without creating a Student object.

What Does Non-Static Mean?

A non-static member belongs to an object of the class.

To use it, I must first create an object.

Example

class Student {
    String name = "John";
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, name is non-static. Every object can have its own value for name.

Static vs Non-Static

Static Non-Static
Belongs to the class Belongs to an object
No object is needed Object is required
Shared by all objects Each object has its own copy
Accessed using the class name Accessed using the object

A Simple Real-Life Example

Imagine a classroom.

  • The school name is the same for every student. So it makes sense to keep it static.
  • The student's name is different for each student. So it should be non-static.
class Student {
    static String school = "ABC School";
    String name;

    Student(String name) {
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

If I create two students:

Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
Enter fullscreen mode Exit fullscreen mode

Then:

  • Student.school"ABC School" (same for everyone)
  • s1.name"Alice"
  • s2.name"Bob"

This helped me understand why static data is shared while non-static data is unique for each object.

Why Is main() Static?

One question I had was:

Why do we write public static void main(String[] args)?

The reason is simple.

When the Java program starts, no objects have been created yet. Java needs a method it can call immediately, so it makes the main() method static.

That's why Java can run the program without creating an object first.

Can a Static Method Access Non-Static Variables?

No, not directly.

For example:

class Test {
    int x = 10;

    static void display() {
        System.out.println(x); // Error
    }
}
Enter fullscreen mode Exit fullscreen mode

This gives an error because x belongs to an object, but the static method doesn't know which object to use.

The correct way is:

class Test {
    int x = 10;

    static void display() {
        Test t = new Test();
        System.out.println(t.x);
    }
}
Enter fullscreen mode Exit fullscreen mode

When Should We Use static?

Use static when:

  • The value is common to every object.
  • You don't need object-specific data.
  • You want to call a method without creating an object.

Examples include:

  • Constants
  • Utility methods
  • The main() method
  • Shared counters

Top comments (0)