Static Keyword In Java

Static Keyword In Java

Introduction

  1. In a java program we have the following things a. static variables b. static block c. static methods

    e. Non-static Variables f. Non-static block g. Non-static methods

Variable Access

  1. If a variable is declared as static then a.It can be accessed in the static method, static block b. It can also be accessed in the non-static method, non-static block
  2. If a variable is declared as non-static then c. It can be accessed in the nonstatic block and nonstatic methods only

How A Java Program gets executed

  1. When we finish writing the Java program the first step in execution is compiling the program
  2. After the compilation is complete then we get a byte code of the program which is then given to the Java Virtual Machine to begin the execution
  3. The JVM is further divided into 3 areas a. Class Loader b. Runtime Data Areas c. Execution Area

jvm-architecture.png

  1. Static Variables are allocated memory by the class loader and default values are assigned
  2. Static Block will also be executed by the class loader
  3. Static Method will be loaded onto the stack of the class Loader, If we have more than two static methods then always the main method is called first
  4. Then the other static methods are called and executed
    Note: We have not created any object while executing the static variables as still we are in the class loader phase and objects are created at runtime and are allocated memory at run-time
  5. After class loading is complete objects are created, When we create an object then the constructor will be called
  6. Suppose we have non - a static block then what will happen before we start executing the statements written inside the constructor of the non-static block will get executed first\
  7. After the non-static block is executed, the constructor is executed
  8. Then the non-static method will be executed
  9. Whenever we create an object every time memory will be allocated for non-static variables
  10. When to use static variables, when we have a common value that is shared between all the instances of the class then we use static variables
  11. Use of static block is to initialize main method and when we want to perform some action before the main method is executed we use static block

Example Of All Above In A Single Code


class HelloWorld {
    HelloWorld(){
        System.out.println("I am constructor ");
    }


    static int a=10;

    static{
        System.out.println("no object created yet");
        System.out.println(a);
    }
    public static void  disp(){
        System.out.println("static block called after main ");
    }

   int b=20;
   {
    System.out.println(b);
    System.out.println(" i AM NON -STATIC BLOCK");
   }

    public static void main(String[] args) {
        System.out.println("calling main");
        disp();
        System.out.println("after creating an object");

        HelloWorld object= new HelloWorld();

        System.out.println("using static variable from non-static block");
        System.out.println(a);



    }


}

Screenshot 2022-12-04 232155.jpg