Static Method in Java

Category: Java   Tags: Java, Java Basic, Java Static Method

A static member can be accessed before any object of its class are created, and without reference to any object. Both methods and variables can be static. All instances of the class share the same static variable.

A static method has following things:

  • They cannot refer to this or super in any way.
  • When objects of its class are declared, no copy of a static variable is made.
  • They can only access static data.
  • They can only call other static methods.

static Block

A static block gets executed exactly once, when the class is first loaded i.e. first time you make an object of that class or the first time you access a static member of that class.

A static block gets executed before constructor get called.

StaticExample.java

                          package com.tutorial.javabasic;

                          class Counter {
                              static int count = 0;
                              static {
                                  count = 10;
                                  System.out.println("static block called");
                              }

                              Counter() {
                                  System.out.println("Constructor called");
                              }

                              public static void print() {
                                  System.out.println("Counter is: " + count);
                              }
                          }
                          public class StaticExample {

                              public static void main(String s[]) {
                                 Counter counter = new Counter();
                                 Counter.count++;
                                 System.out.println("Print count inside main: " + Counter.count);
                                 Counter.print();
                              }
                          }
                        

Output:

                          static block called
                          Constructor called
                          Print count inside main: 11
                          Counter is: 11
                        

As we said earlier:

A static block gets executed exactly once, when the class is first loaded i.e. first time you make an object of that class or the first time you access a static member of that class.
Therefore, when we create an object of our class Counter using Counter counter = new Counter(); statement, below static block is get called:

                          static {
                              count = 10;
                              System.out.println("static block called");
                          }
                        

Here, count is initialized to 10. From the output of this program you can see that the line static block called is printed before Constructor called that mean a static block is executed before a constructor.

You can see that a static variable and method can be access directly using its class name:
Counter.count++;
Counter.print();