DEV Community

Cover image for Static and Non-Static in Java
ABISHEK M
ABISHEK M

Posted on

Static and Non-Static in Java

1. Introduction

In Java, static and non-static are important concepts in Object-Oriented Programming (OOP).

They help us understand how variables and methods belong to a class or an object.

Java provides the static keyword to declare class-level members.

There are two categories:

  1. Static members
  2. Non-static (instance) members

2. What is Static in Java?

Static means a member belongs to the class rather than an individual object.

We use the static keyword to declare static variables, methods, and blocks.

Example: Static Variable

class Student {

    static String college = "ABC College";

    public static void main(String[] args) {

        System.out.println(Student.college);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

ABC College
Enter fullscreen mode Exit fullscreen mode

Explanation

  • college is a static variable.
  • It belongs to the Student class.
  • All Student objects share the same static variable.
  • We can access it using the class name without creating an object.
Student.college;
Enter fullscreen mode Exit fullscreen mode

3. What is Non-Static in Java?

Non-static members belong to individual objects.

A variable or method declared without the static keyword is generally an instance member.

Example: Non-Static Variable

class Student {

    String name;
    int age;

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Abishek";
        s1.age = 21;

        System.out.println(s1.name);
        System.out.println(s1.age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
21
Enter fullscreen mode Exit fullscreen mode

Explanation

  • name and age are instance variables.
  • They belong to the Student object.
  • Each object gets its own copy of these instance variables.
  • We need an object reference to access them.

4. Real-World Example: College Management System

Imagine a college with 1,000 students.

Every student has:

  • A different name.
  • A different age.
  • A different roll number.
  • The same college name.

We can represent this using static and non-static variables.

Student Name Age College
Student 1 Abishek 21 ABC College
Student 2 Arun 22 ABC College
Student 3 Kumar 20 ABC College

Here:

  • Name → Non-static
  • Age → Non-static
  • Roll number → Non-static
  • College name → Static

Java Implementation

class Student {

    String name;
    int age;

    static String college = "ABC College";

    void display() {

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("College: " + college);
    }

    public static void main(String[] args) {

        Student s1 = new Student();
        Student s2 = new Student();

        s1.name = "Abishek";
        s1.age = 21;

        s2.name = "Arun";
        s2.age = 22;

        s1.display();
        s2.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Abishek
Age: 21
College: ABC College
Name: Arun
Age: 22
College: ABC College
Enter fullscreen mode Exit fullscreen mode

5. Static Method in Java

A method declared using the static keyword is called a static method.

Static methods belong to the class and can be called without creating an object.

Example

class Calculator {

    static void add() {

        int a = 10;
        int b = 20;

        System.out.println(a + b);
    }

    public static void main(String[] args) {

        Calculator.add();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Here, we call the static method using the class name.

Calculator.add();
Enter fullscreen mode Exit fullscreen mode

We don't need to create an object.

6. Non-Static Method in Java

A method declared without the static keyword is called a non-static method or instance method.

To call a non-static method, we need an object.

Example

class Calculator {

    void multiply() {

        int a = 10;
        int b = 20;

        System.out.println(a * b);
    }

    public static void main(String[] args) {

        Calculator c1 = new Calculator();

        c1.multiply();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

200
Enter fullscreen mode Exit fullscreen mode

Here:

  • multiply() is a non-static method.
  • c1 is an object of the Calculator class.
  • We use the object reference to call the method.

7. Difference Between Static and Non-Static

Feature Static Non-Static
Belongs to Class Object
Keyword static No static keyword
Object required? No Yes, for instance access
Variables Shared among objects Separate for each object
Methods Can be called using class name Called using object reference
Example static int count; int age;

8. Why Is the Main Method Static in Java?

This is one of the most frequently asked Java interview questions.

Consider this program:

class Student {

    public static void main(String[] args) {

        System.out.println("Hello Java");
    }
}
Enter fullscreen mode Exit fullscreen mode

When we execute:

javac Student.java
java Student
Enter fullscreen mode Exit fullscreen mode

We get:

Hello Java
Enter fullscreen mode Exit fullscreen mode

But we never created a Student object to call the main() method.

Why?

Because the main method is static.

Explanation

The Java launcher starts the application and invokes its entry-point method.

Since main() is static, it can be invoked without creating an object of the Student class.

If the main method were non-static, an object would be required to call it.

For example:

Student s1 = new Student();
s1.main(args);
Enter fullscreen mode Exit fullscreen mode

However, the traditional Java application launcher expects a valid static main method as its entry point.

Therefore, we declare:

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

Meaning of Each Keyword

Keyword Explanation
public Allows the launcher to access the method
static Allows invocation without creating an object
void Does not return any value
main Name of the traditional application entry-point method
String[] args Receives command-line arguments

9. How Does the JVM Execute the Main Method?

When we execute:

java Student
Enter fullscreen mode Exit fullscreen mode

The Java application startup process happens approximately as follows:

  1. The Java launcher starts the runtime.
  2. The JVM loads the required class.
  3. The class is linked and initialized.
  4. The launcher identifies the valid main() entry point.
  5. The launcher invokes the main method.
  6. Statements inside the main method execute.

Execution Flow

Java Source Code
       |
       v
   javac Compiler
       |
       v
   Bytecode (.class)
       |
       v
   java Command
       |
       v
      JVM
       |
       v
   Class Loading
       |
       v
 Class Initialization
       |
       v
  Main Method
       |
       v
 Program Execution
Enter fullscreen mode Exit fullscreen mode

10. Why Can't a Static Method Directly Access a Non-Static Variable?

Consider this example:

class Student {

    String name = "Abishek";

    public static void main(String[] args) {

        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

This code produces a compilation error.

Why?

  1. main() is a static method.
  2. name is a non-static variable.
  3. The non-static variable belongs to an object.
  4. The static method does not automatically have an object reference.

Correct Approach

class Student {

    String name = "Abishek";

    public static void main(String[] args) {

        Student s1 = new Student();

        System.out.println(s1.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
Enter fullscreen mode Exit fullscreen mode

Important: A static method can access non-static members through an object reference.

11. Static Block in Java

A static block is a block of code declared using the static keyword.

It executes during class initialization, before the main method is invoked when that class is the application entry point.

Example

class Employee {

    static {
        System.out.println("Static Block");
    }

    public static void main(String[] args) {

        System.out.println("Main Method");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Static Block
Main Method
Enter fullscreen mode Exit fullscreen mode

Explanation

During class initialization, Java executes static field initializers and static blocks in their textual order.

After class initialization, the launcher invokes the main method.

Another Example

class Test {

    static int a = 10;

    static {
        a = 20;
    }

    public static void main(String[] args) {

        System.out.println(a);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

20
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Static variable a is initialized to 10.
  2. Static block changes a to 20.
  3. Main method prints the value.
  4. Final output is 20.

12. Important Rules to Remember

  1. Static members belong to the class.
  2. Non-static members belong to individual objects.
  3. Static variables are shared among objects of the same class.
  4. Each object has its own instance variables.
  5. Static methods can directly access static members.
  6. Static methods cannot directly access instance members without an object reference.
  7. Non-static methods can access both static and non-static members.
  8. The traditional Java main method is static so the launcher can invoke it without creating an instance.
  9. Static blocks execute during class initialization.

13. Conclusion

Static and non-static are fundamental concepts in Java.

Static members are useful when data or functionality is common to all objects of a class. Non-static members are useful when each object needs its own data or behavior.

Understanding these concepts helps us write better Java programs and understand how the JVM starts and executes a Java application.

Top comments (0)