Static Keyword in Java Programming

Static keyword in Java is a non-access modifier that helps to modify the meaning of a variable, method, or class.

Java contains various types of keywords or modifiers. Some of them include the following

  • Final
  • Static
  • Abstract
  • Interface
  • Using the static keyword in Java makes a method or variable belong to the class rather than an individual object. The programmer can change the value of a static variable unless additional rules prevent it.
  • However, the program can change the value stored in memory.
  • Even without creating an object, it is possible to access static members of a class.
  • Static members are called class members.
An introduction to STATIC KEYWORD IN JAVA PROGRAMMING LANGUAGE

Static keywords are known either as a keyword or modifier.

Some Key Points of Static Keyword in Java

  • The Static keyword is only used for the Data Members and Member functions.

class Demo {
    static int a;
    static float b;
    static double c;  // Data Members

    public static void Method1() {
        System.out.println("Method 1");
    }  // Member Functions

    public static void Method2() {
        System.out.println("Method 2");
    }
}

Use case of static keyword with Data Members and Member Functions

  • All Static Members are first to execute.
  • However, do not use static keywords along with Classes and Constructors.
  • Consequently, there are no STATIC classes in Java

Do not use Static Keywords with Class or Constructor

1. Static keyword with a class (Invalid)


static class Demo {
    // ...
}

Error: modifier static not allowed here

2. Static keyword with a constructor (Invalid)


class Demo {
    static Demo() {
        a = 10;
    }
}

Error: modifier static not allowed here

Static keywords shows errors with classes or constructors

  • If there is a body for the static keyword, it is known as Static Block.

STATIC BLOCK – has the highest PRECEDENCE among all the declarations of the class. It means it executes first, even before the static keyword.


class Example {
    static int a;
    float b;

    Example() {
        a = 10;
        b = 5.5f;
    }

    static {
        System.out.println("Static Block...");
        a = 25;
        m1();
    } // Static block has the highest precedence

    public static void m1() {
        System.out.println("Sum is : " + (a + 10));
    }

    public void m2() {
        System.out.println("Product is : " + (a * b));
    }

    public static void main(String[] args) {
        Example sd = new Example();

        System.out.println("a value : " + sd.a);
        m1();
        sd.m2();
    }
}

Output


Static Block...
Sum is : 35
a value : 10
Sum is : 20
Product is : 55.0
  • NON-STATIC members CANNOT be referenced from a STATIC context.

class Example {
    int a;
    static float b;
    static double c;

    public static void Method1() {
        System.out.println("A value: " + a);
    }
}

Output


error: non-static variable a cannot be referenced
       from a static context

System.out.println("A value: " + a);
                                  ^
  • All STATIC members CAN be referenced from a NON-STATIC context.

class Demo {
    static int a;

    Demo() {
        a = 6;
    }

    public static void Method1() {
        System.out.println("A value: " + a);
    }

    public void Method2() {
        System.out.println("A value: " + (a + 30));
    }

    public static void main(String[] args) {
        Demo d = new Demo();

        d.Method1();
        d.Method2();
    }
}

Program Output


A value: 6
A value: 36
  • All NON-STATIC members participate in every instance of the class
  • In order to ACCESS NON-STATIC members from a STATIC CONTEXT – we require an OBJECT

class Demo {
    int a;

    Demo() {
        a = 6;
    }

    public void Method1() {
        System.out.println("A value: " + a);
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.Method1();
    }
}


Output

A value: 6

  • Superclass ‘static’ method CAN be Overridden as ‘static’ ONLY

class DemoParent {
    static int a = 10;

    public static void Method1() {
        System.out.println("A value: " + a);
    }
}

class Demo extends DemoParent {
    public static void Method1() {
        System.out.println("A value: " + (a + 10));
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.Method1();
    }
}

Output


A value: 20
  • It is not possible to declare Abstract methods as ‘static’

abstract class DemoAbstract2 {
    public abstract static void Method1();
}

class DemoAbstract extends DemoAbstract2 {
    public void Method1() {
        System.out.println("Hello World!");
    }

    public static void main(String[] args) {
        DemoAbstract d = new DemoAbstract();
        d.Method1();
    }
}

Output


DemoAbstract.java:4: error: illegal combination of modifiers:
abstract and static
    public abstract static void Method1();
                               ^
1 error
  • Thus, it is possible to access all static members outside the class using its class name.
  • Finally, the programmer can import packages statically

Leave a Comment

Discover more from BerylSoft

Subscribe now to keep reading and get access to the full archive.

Continue reading