Static variable, method and block in Java

Posted on

 Static variable, method and block

         Static variable :

  • Static variable’s value is same for all the object of the class i e if the value of the variable does not change from object to object we have to declare such type of variables at class level by using static
  • Static variables are used to create constant value attached to the class, and final keyword can be used to make the values constant i e to prevent it from ever being changed

               Eg : public final static int x = 10;

  • In the case of static variable a separate copy will be created at class level and shared by every object of the class.
  • Static variables should be declared within the class directly but outside of any method, block or constructor.
  • Static variables are created at the time of class loading and destroyed at the time of class unloading, hence the scope of static variable is exactly same as the scope of .class
  • Static variables are stored in method area.
  • We can access static variables either by object reference or by class name but recommended to use class name.
    • Within the same class it is not required to use class name and we can access directly.

Eg :

class staticDemo

{

       static int x=10;

       Public static void main (String[] args)

{

staticDemo d =new staticDemo ();

System.out.println(d.x); //(valid but not recommended)

System.out.println(staticDemo.x); // (valid but used in different class )

System.out.println(x); // ( valid within same class)

}

}

—————————————————————————————————————————————-

Output : 10

                  10

                  10

—————————————————————————————————————————————-

 

  • We can access static variables directly from both instance and static areas.

 

Eg :

   class staticDemo

{

Static int x = 10;

public static void main (String[] args)

{

System.out.println(x); // (valid in static area)

}

public void m1( )

{

System.out.println(x); // (valid in instance area )

}

}

—————————————————————————————————————————————-

Output : 10

—————————————————————————————————————————————

 

  • JVM provides default values to the variables and we are not required to perform initialization to the variables explicitly.e. for numbers, the default value is 0, for Booleans it is false, and for object references it is null.Values can be assigned during the declaration or within the constructor.

 

Eg :

class Test

{

           static int x;

           static boolean b;

           static String s;

public static void main( String[] args)

{

              System.out.printl(x);

              System.out.printl(b);

              System.out.printl(s);

}

}

—————————————————————————————————————————————-

Output : 0

                  false

                  null

—————————————————————————————————————————————-

  • Static variables are also known as class level variables or fields.

      Static methods :

  • It is a method which belongs to the class and not to the object (instance).
  • A static method can access only static data. It can not access non-static data (instance variables)
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can be accessed directly by the class name and doesn’t need any object.
  • A static method cannot refer to this or super keywords in anyway.
  • we write static keyword when defining main it is because program execution begins from main and no object has been created yet.

E.g.

class StaticDemo

                {

               static void display()

                              {

                              System.out.println(“static method”);

                              }

               void show()

                              {

                              System.out.println(“non static method”);

                              }

public static void main(String[] args)

{

   display();  //calling without object

   StaticDemo d = new StaticDemo();

    d.show();  //calling using object

  }

}

—————————————————————————————————————————————–

Output : static method

 Non static method

—————————————————————————————————————————————–

     Static block :

  • Static blocks will be executed at the time of class loading. Hence at the time of loading if we want to perform any activity we have to define that inside a static block.
  • static block (also called static clause) which can be used for static initialization of a class.
  • The code inside static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class).
  • A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
  • If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.
  • If you’re referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM same as above i.e., now the static variables/methods referred and the static block both will be executed.

 

E.g. :

 

class StaticBlock{

   static int num;

   static String str;

   static{

      System.out.println(“first static block”);

      num = 10;

      str = “ten”;

  }

  static{

      System.out.println(“second static block”);

      num = 20;

      mystr = “twenty”;

  }

  public static void main(String args[])

  {

      System.out.println(“number =”+num);

      System.out.println(“String =”+str);

   }

}

 —————————————————————————————————————————————

Output : first static block

                 Second static block

                 number = 20

                 String = twenty

—————————————————————————————————————————————-