Static keyword in Java

In this tutorial, we will learn about Static keyword in Java.

In Java a Class member can be accessed only with an object of its class. Most of the times you want to define a class member that will be used independently of any object of that class. In short you want to create global variables.
To create such a member, prefix its declaration with the Static keyword. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. Member variables declared as static are called as Global variables.

Static keyword can be applied to:

  • Variables
  • Methods
  • Block
  • Nested Class

Let’s see how to apply Static keyword to variables, methods, blocks and nested classes with an example.

1) Static Variable:

Static variables are mostly used to create class level variables. They are also used with final keyword to create constants. Static variables are not thread-safe since they are shared across all the instances of an object.

Static Variable Example:

Output:

Here we have seen that, static variable name does not require an object to access it.

Advantages of using Static Variable:

Static variable usage in the program saves the memore and makes the program memory efficient.

2) Static Method:

When a method in java is prefixed with Static keyword, it is known as Static Method. Similar to static variable, static method is also accessed without creating an object. The best example of Static Method is main() method.

Key points to note about Static Method:

  • Static methods can access only other static methods
  • Static methods can access only other static member variables.
  • this and super keyword cannot be used with static keyword.

Static Method Example:

Output:

In the above program, display() method is invoked without creating an object of the class.

3) Static Block:

In Java Static Block is a group of statements that gets executed when the class is loaded into memory by Java ClassLoader. Static block is used to initialize static variables of the class.

Static block cannot access non-static variables. Static block code is executed only once before main method execution at the time of classloading.

Static Block Example:

Output:

If you observe the output, static block is executed before main method and static variable is initialized in static block.

4) Nested Class:

We know that static keyword cannot be used with outer classes, but we can use it with inner classes also called as nested classes. These nested classes with static keyword are called as static nested class.

Static Nested Class Example:

Output:

If you see the above program, StaticNestedClass is a static nested class inside StaticNestedClassDemo.java class. If you observe the output, outer class static variable count is accessed inside static nested class. The point to note that the non static method display() cannot be accessed in static nested class. So, static nested class can access only static members of the outer class.

Other Tutorials:

Related posts

Leave a Comment