These members execute independently without using class instance.
To create such members Java supports a static keyword that allows them to get executed before object creation.
A Static keyword can be imposed on both methods and variables.
The main method is a classic example of a static keyword that doesn’t need to the object for execution instead it runs before an instance is created.
To access a static variable or to invoke a static method dot operator is used with the class name to which that particular variable or method belongs.
When a variable is declared with the static keyword, it acts like a global variable.
In other words, All the objects share the same variable which gets a single copy in memory, unlike instance variable which gets memory each time when a new object is made.
Static variables initialized with the same default values as instance variables do.
Syntax:
static dataType variableName;
When a method is preceded with a static keyword then it can only access static variables and call the static method right away.
It doesn’t allow this or super keyword as they are references of superclass or subclass.
A static method can be invoked using the class name.
For example, className.static_method(). static methods cannot be overridden.
Syntax:
static return_type method_name(){ //body }
public class Test { //Static variable static int y; Test () { System.out.println ("Hi!I am constructor"); } // Static method to access static variable static void example () { y = 10; System.out.println ("Hi! I am static and can access static variable =" + y); } public static void main (String[]args) { Test obj = new Test (); Test.example (); } }
Output:
Hi!I am constructor Hi! I am static and can access static variable = 10
When a pair of curly braces is followed by a static keyword, then it is referred to as static initializer or static initialization block.
It executes only once for respective class, rather then for every object.
As the name suggests it is used to initialize static data members only. These types of blocks don’t have return statement and can’t throw checked exceptions.
Syntax:
static{ //static variable initializer }
public class Test { //Static variable static int y; Test () { System.out.println ("Hi!I am constructor"); } // Static block to initialize variable static { y = 10; System.out.println ("I am static Initializer = " + y); } public static void main (String[]args) { Test obj = new Test (); } }
Output:
I am static Initializer = 10 Hi!I am constructor
There is a concept of nested class which can be defined as a class when declared within another class is known as a nested class and when this nested class is preceded with a static keyword then it is said to be a static nested class.
In other words, a static nested class is a static member of the outer class.
It doesn’t require to wait for the instantiation of an object of the outer class which means we can create an object of static inner class independently.
Syntax:
class A { static class B { } }
public class Test { //Static variable static int y; // constructor Test () { System.out.println ("Hi!I am constructor"); } static class nestedClass { void display () { System.out.println ("I am from static nested class"); } } public static void main (String[]args) { Test obj = new Test (); Test.nestedClass o = new Test.nestedClass (); o.display (); } }
Output:
Hi!I am constructor I am from static nested class
Program:
class Test { static int y; //Static variable Test () // constructor { System.out.println ("Hi!I am constructor"); } static class nestedClass //Nested static class { void display () { System.out.println ("I am from static nested class"); } } // Static block to initialize variable static { y = 10; System.out.println ("I am static Initializer = " + y); } static void example () // Static method to access static variable { y = 10; System.out.println ("Hi! I am static and can access static variable =" + y); } public static void main (String[]args) { Test obj = new Test (); Test.nestedClass o = new Test.nestedClass (); // object of inner class Test.example (); o.display (); } }
Output:
This post was last modified on July 4, 2022