static Keyword in Java with Examples

In this article, we will discuss a lot about static keyword and it's usage with respect to variables, methods, blocks and nested class.

The static keyword in Java is used a lot in java programming. We can apply java static keyword with variables, methods, blocks and nested class.

1. Java static Variable

We can use a static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class.

A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

static variable example

Consider these are 100 students in a college named "ABC", each student have their own unique roll number and name but the college remains the same among all the 100 students. The college field is declared as static so it can occupy memory only once.

6. Java static import

Java 5 introduced a new feature — static import — that can be used to import the static members of the imported package or class. You can use the static members of the imported package or class as if you have defined the static member in the current class.