The Static keyword in Java is used mainly for memory management. The static keyword belongs to the class than an instance of the class, which means if you make a member as static then you can access it without creating a class object.
The static keyword can be used as :
- A variable
- A method
- A class
- A Block of statements
1. Java Static variable
It is common to all instances(or objects) of the class because it is a class level variable. It also means that the static variable is created only once and is shared among all the other instances of the class. A static variable can be accessed directly by the class name and doesn’t need any object.
<class-name>.<variable-name>
When and why we use static variable?
Scenario –
- Things like Database connection url , which may not change for a particular database
- An employee class, it has many employees with unique employee ids but belongs to the same company, hence company name can be put into a static variable.
Example:
public class StaticVariableTestClass {
static int a1;
static String a2;
// This is a Static Method, calling static variables
static void display() {
System.out.println("Variable one is: " + a1);
System.out.println("Variable two is: " + a2);
}
public static void main(String args[]) {
display();
}
}
OUTPUT
Variable one is: 0
Variable two is: null
Another Usecase : counter ++ using static variable, used as a counter variable.
2. Java Static method
- Static method in Java is a method which belongs to the class and not to the object. A static method can access only static data(not the instance variables)
- A static method can call only other static methods from it and cannot call a non-static method.
- A static method cannot refer to “this” or “super” keywords.
- A static method can be accessed directly by the class name , object instance is not needed.
<class-name>.<method-name>
public class TestStaticVariable {
static int sum;
// This is a Static Method
static int sumOfTwoNumbers(int a1, int a2) {
sum=a1+a2;
return sum;
}
public static void main(String args[]) {
sum=sumOfTwoNumbers(5,5);
System.out.println("The sum of two numbers is : "+sum);
}
}
OUTPUT
The sum of two numbers is : 10
3. Java Static block
- Static block is a block where you can write statements to declare static variables.
- Static block is the first thing which JVM initializes on class load.
public class StaticBlock {
static int number;
static int square;
// This is a Static Block
// No matter where it is placed(top or bottom of code) it gets called
// before any other method
static {
System.out.println("** inside static block to initialize static variable **");
number = 5;
}
// This is a Static Method
static int squareOfANumber(int a1) {
square = a1 * a1;
return square;
}
public static void main(String args[]) {
square = squareOfANumber(number);
System.out.println("The square of a " + number + " is : " + square);
}
}
OUTPUT
** inside static block to initialize static variable **
The square of a 5 is : 25
4. Java Static class
- A class can be made static only if it is a nested class which also means We can not declare top-level class with a static modifier.
- Nested static class doesn’t need reference of Outer class.
- A static class cannot access non-static members of the Outer class.
- A non static nested class is called the inner class.
public class OuterClass {
private static String str = "ProgrammerToday";
// nested class - as it is (inner + static)
static class NestedStaticClass {
// static method inside nested class
static void displayName() {
System.out.println("Book Name is : " + str);
}
// non-static method inside nested class
void displayContent() {
System.out.println("This book is about technology");
}
}
public static void main(String[] args) {
// way to call static method of nested class
OuterClass.NestedStaticClass.displayName();
// way to call non-static method of nested class by creating instance
OuterClass.NestedStaticClass obj = new NestedStaticClass();
obj.displayContent();
}
}
OUTPUT
Book Name is : ProgrammerToday
This book is about technology
Summary
In this article we learnt about Java Static Keyword and its various use cases. We sar it can be used as a variable, method, block and a class.
Hope you liked the article !