static keyword in java

In Java, the static keyword is used to create members (fields, methods, and nested classes) that belong to the class rather than to instances of the class. This means that a static member is associated with the class itself, not with objects created from the class.

Here are the main uses of the static keyword:

  1. Static Fields:
  • Static fields, also known as class variables, are shared among all instances of a class.
  • They are declared using the static keyword.
   class MyClass {
       static int staticField = 10;
       int instanceField = 20;
   }

Accessing a static field:

   int value = MyClass.staticField;
  1. Static Methods:
  • Static methods belong to the class rather than an instance of the class.
  • They are declared using the static keyword.
  • They can be called using the class name, without creating an instance of the class.
   class MyClass {
       static void staticMethod() {
           System.out.println("This is a static method.");
       }
   }

Calling a static method:

   MyClass.staticMethod();
  1. Static Block:
  • A static block is a block of code inside a class that is executed only once when the class is loaded into the memory.
  • It is declared using the static keyword.
   class MyClass {
       static {
           System.out.println("This is a static block.");
       }
   }

The static block is executed when the class is first accessed, before any static methods or fields are used.

  1. Static Nested Classes:
  • A static nested class is a class that is defined within another class and marked as static.
  • It does not have access to the instance-specific members of the outer class.
   class OuterClass {
       static class StaticNestedClass {
           // ...
       }
   }

Accessing a static nested class:

   OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
  1. Static Import:
  • The static keyword is also used in the context of static imports to allow the importing of static members of a class.
   import static java.lang.Math.PI;

This allows you to use PI directly without qualifying it with the class name.

Static members are shared among all instances of a class, and they can be accessed using the class name without creating an object. They are useful for representing shared resources, constants, utility methods, and more.

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object

We can declare both methods and variables to be static.

Methods declared as static have several restrictions

  • They can only call other static methods.
  • They must only access static data
  • They cannot refer to this or super in any way

Example

class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes, which prints a message and then initializes b to a*4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Output

Static block initialized.
x = 42
a = 3
b = 12

Leave a Comment