Static keyword in Java with example

Static keyword in Java with example

The static keyword denotes that a member variable, or method, can be accessed without requiring an object of the class to which it belongs.

The static keyword can be applied to:

  1. Variable (also known as class variable)
  2. Method (also known as class method)
  3. Initialization block
  4. Nested class

Static keyword can not be applied to

  • Top level Class (Not Nested)
  • Constructor
  • Interfaces
  • Method Local Inner Class(Different then  nested class)
  • Inner Class methods
  • Instance Variables
  • Local Variables

Static Variable

Static variables also called as class level variables that are common to all the objects of the class. Memory allocation for such variables only happens once when the class is loaded in the memory. These variables can be accessed using class name. Unlike non-static variables, such variables can be accessed directly in static and non-static methods.

Example

As you can see in the above example both the employee objects share the same company.

Static Methods

A static method belongs to the class rather than an object of a class. Static methods can be invoked without the need for creating an instance of a class. It can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Note that static methods cannot be overridden and this and super cannot be used in static context

Example

In the above example you can see main method which itself is a static method is able to access the static method message() directly without the need to create an object. It can also be noted that, to access a non static method(display()) inside a static method, you need an object.

Static Initialization block

Static initialization block is used to initialize static data members. Static initialization block gets executed when the class is loaded into the memory(so it is executed even before the main method). Creating multiple instances of a class doesn’t mean that Static block will get executed multiple times. A class can have multiple Static blocks, which will execute in the same order they appear in the program.

Example

Static Class

A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class. A top level class cannot be made static. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class. A static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Example

It can be noted from the above example that, we have created an object of the static class to call its instance method display(), whereas the static print() can be called directly with the class name(in this case the static class name has to be accessed with the outer class name).

What is the use of a Static class?

A good use of a static class is in defining utility and/or library classes where instantiation would not make sense. A great example is the java.lang.Math class that contains some mathematical constants such as PI and E and simply provides mathematical calculations. Requiring instantiation in such a case would be unnecessary and confusing.

Hope you find this post useful.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.