Tutorials

Java Keywords


Java Static Keyword


In this chapter of the java tutorial we will learn about the use of static keyword.

Static can be used with:

  1. variable
  2. method
  3. block

We will learn about each of them one by one.

 

Static modifier when applied with either method or variable has a very profound impact on their behaviour.

 

Variables and Methods marked static belongs to the class.

Static variables and methods can be accessed without creating the instance of the class.

 

This is the reason main() method is declared static so JVM can directly access it without the need of instantiating the class in which it is present.

Important points on static

  • We can access static method and static variable using dot (.) operator directly on class name(see example below).
  • You cannot access non-static variable or non-static method from static method directly. 
  • this keyword cannot be used with static context.
  • super keyword cannot be used with static context.

 

We have already discussed about static variable in variables section of the tutorial. Please click to read.

 

Here we will first discuss in detail about static method.

 

Java static method example

Given below is example showing static method.

static method by tutorialsinhand.com

 

Since add method is static so it was easily called from main method which is also static. 

 

Now make add method non-static(remove static keyword). Once you remove static keyword from add method, compiler will report error - "Cannot make a static reference to the non-static method add(int, int) from the type StaticExample"

Java Static Block

Static block in java class is written as shown below:


static {

//your code goes here

}

Important point on java static block 

  1. static block is executed during classloading thus is executed even before main() method is called.
  2. static block is mostly used to initialize static variables.
  3. A java class can have multiple static block.
  4. If a java class has multiple static block they would get executed in the sequence they are introduced.

Example on static block

Given below is exampe of static block

package StaticPack;
//static block example by tutorialsinhand.com
public class StaticBlockExample {

  static{
   System.out.println("For java version less than 1.7");
   System.out.println("static block will get execued before main() method");
  }

  public static void main(String[] args) {
   System.out.println("main executed");
  }
}

OUTPUT:
For java version less than 1.7
static block will get execued before main() method
main executed

 

From the above code, we can easily find after reading the output that static bloc is executed before the main() method.

Similarly we can have multiple static blocks in our java code and they would get executed in sequence they appear in the code.



Please Share this page
Views : 196
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!