Java static keyword

In this tutorial, we will learn how to use the Java static keyword.

1. What is a static keyword in Java?

In Java, if we want to access class members, we usually must first create an instance of the class. However, there are situations where we want to access class members without creating an instance. In those situations, we declare the class members static.

// static method
static void myStaticMethod() { ... }

Can a static method access non-static variables?

Here, myStaticMethod() is a static method. It can be called without creating an object of the class. The Math class in Java has almost all of its members static. So, we can access its members without creating instances of the Math class.

Tip 💡: static means "Shared by everyone". Like the office coffee machine. You don't need your own personal coffee machine (object) to get coffee!

Example 1

public class Main {
    public static void main( String[] args ) {

        // accessing the methods of the Math class
        System.out.println("Absolute value of -12 =  " + Math.abs(-12));
        System.out.println("Value of PI = " + Math.PI);
        System.out.println("Value of E = " + Math.E);
        System.out.println("2^2 = " + Math.pow(2,2));
    }
}

Output:

Absolute value of -12 = 12
Value of PI = 3.141592653589793
Value of E = 2.718281828459045
2^2 = 4.0

In the above example, we have not created any instances of the Math class. But we are able to access its methods (abs() and pow()) and variables (PI and E).

It is possible because the methods and variables of the Math class are static.

If you change the value of a static variable in one object, does it change for all other objects of that class?


2. Static Blocks

Static blocks are used to initialize static variables. They are executed only once when the class is loaded into memory.

class Main {
    static int a = 10;
    static int b;

    // static block
    static {
        System.out.println("Static block initialized.");
        b = a * 4;
    }

    public static void main(String[] args) {
        System.out.println("Value of a: " + a);
        System.out.println("Value of b: " + b);
    }
}

Output:

Static block initialized.
Value of a: 10
Value of b: 40

Challenge

Complete this chapter to unlock the next one.

Challenge

Task:

Create a class 'Counter' with a static variable 'count'. Increment it in the constructor. Create 2 objects and print 'count'.

Key Takeaways

  • Class Level: Belongs to the class, not individual objects.
  • Shared: All objects share the SAME static variable.
  • Utility: Static methods are great for helper functions (like Math.sqrt()).

Common Pitfalls

[!WARNING] Static vs Instance: You CANNOT access non-static (instance) variables from a static method. The static method doesn't know which object's variable to use!

[!WARNING] Memory: Static variables stay in memory for the entire life of the program. Use them wisely.

What's Next?

We've covered the core of OOP! Now let's handle errors gracefully. Learn Exception Handling →