The static keyword is mostly used in Java for memory management. Variables, methods, blocks, and nested classes are all supported. It is a keyword that is used to share a class's same variable or method. Static refers to a constant variable or method that is the same for all instances of a class. A class's main method is usually tagged static.
You must precede the declaration of a static member (block, variable, method, nested class) with the term static. When a class member is marked static, it can be accessed before the class's objects are created and without requiring an object reference.
The static keyword in the Java programming language is a non-access modifier that can be used for the following:
Static Block
Static Variable
Static Method
Static Classes
When a member is marked as static, it can be accessed without the use of an object. This signifies that the static member is active and available before the class is instantiated. Unlike other non-static class members, which vanish when the class's object is removed from scope, the static member remains visible.
If you need to compute something to initialise your static variables, you can create a static block that is only run once, when the class is loaded. To further understand how to use Static Block, look at the Java programme below.
import java.util.*; public class Main { // static variable static int i = 5; static int j; // static block static { System.out.println ("Intialzation of static block"); j = i + 10; } public static void main (String[]args) { System.out.println ("main method"); System.out.println ("Value of i : " + i); System.out.println ("Value of j : " + j); } }
When a variable is declared static, a single copy of the variable is produced at the class level and distributed across all objects. On the other hand, static variables are effectively global variables. Essentially, the class's instances all share the same static variable. Only at the class level may static variables be created.
import java.util.*; public class Main { // static variable static int p = q(); // static block static { System.out.println("static block"); } // static method static int q() { System.out.println("from method q"); return 100; } public static void main(String[] args) { System.out.println("Value of p : "+p); System.out.println("Main method"); } }
A static method is one that has been declared using the static keyword. The most typical example of a static method is the main() method.Static methods are subject to the following limitations:
Only other static methods can be called directly by them.
They have immediate access to static data.
class Main { // static variable static int num1 = 100; // instance variable int num2 = 200; // static method static void method1() { num1 = 200; System.out.println("from method1"); // Cannot make a static reference to the non-static field b num2 = 100; // compilation error // Cannot make a static reference to the // non-static method method2() from the type Test method2(); // compilation error // Cannot use super in a static context System.out.println(super.num1); // compiler error } // instance method void method2() { System.out.println("from method2"); } public static void main(String[] args) { // main method } }
Inner classes can be declared with the static modifier, and these inner classes are referred to as static nested classes. If there is no existing outer class object for a static nested class, there is a potential that a static nested class object will exist.
class Main { static class Inner { public void method1() { System.out.println("Inside nested class method"); } } public static void main(String[] args) { Main.Inner m=new Main.Inner(); m.method1(); } }