Static & Non-Static Block

In this section we will discuss about the static and Non-Static block in java. There are 2 types of block in java. One is Static & other is Non-Static. Let’s discuss about these blocks in details.

Objectives:

  • Block in Java
  • Static Block
  • Non-Static block
  • Order of Execution
  • Examples
  • Scope of variable in block

Block in Java:

Block is a special type or component in java which placed only inside the class but outside the method. We cannot place this inside the method. It starts with ‘ { ’ and ends with ‘ } ‘. Inside the braces we can have one or more than one statement. we can use a block generally to group several statements in java.

Syntax:

{
// Java Codes;
}

There are 2 types of block in java.

  • Static Block
  • Non-Static Block

Static Block:

If the block in declared with the static keyword then this block is known as Static block. To specify static keyword to block, we need to mention the static keyword before starting ‘ { ‘.

Static block always executes before the main method. That means when you will start the execution the class, it always executes first static block and then it moves to main method. In other way we can say that, the code of the static block will run at class loading time. If there are multiple static block presents in a single class then the order of execution will the order in which they are defined. After all static block execution completes, main method will execute.

Syntax:

static {
//Java code ;
--------------------------
--------------------------
--------------------------
}

Example:

class StaticTesting{
	static {
		System.out.println("We are in static Block");
	}
	public static void main(String args[]){
		System.out.println("We are in main method");
	}
}

Output:

We are in static Block
We are in main method

Explanation:

From the above example we are seeing that, before main method static block is executed.

Can we execute the program without mentioning anything in main method? Let’s see this with a simple example.

Example:

class StaticTesting{
	static {
		System.out.println("Let's start with a calculation without mentioning anything in main method");
		int x=5;
		int y=4;
		int z= x+y;
		System.out.println("Sum is : " + z);	
	}
	public static void main(String args[]){
		
	}	
} 

Output:

Let's start with a calculation without mentioning anything in main method
Sum is : 9

Explanation:

Here we can see we have not mentioned anything inside the main method. All calculation we have defined inside the static method. Still we are getting output as expected.

So, can we execute only static block without main method?

Absolutely No. We cannot execute a java method without main method.

Example:

class StaticTesting{
	static {
		System.out.println("Let's start with a calculation without mentioning anything in main method");
		int x=5;
		int y=4;
		int z= x+y;
		System.out.println("Sum is : " + z);	
	}	
} 

Output:

Static & Non-Static Block

Example:

class StaticTesting{
	static {
		System.out.println("Static block 1");	
	}
	public static void main(String args[]){	
		System.out.println("Main Method");
	}
	static {
		System.out.println("Static Block 2");			
	}	
}

Output:

Static block 1
Static Block 2
Main Method

Explanation:

From the above example we can notice that we have declared 2 static block and one main method. Even if second static block is defined after the main method, it is executing before main method.

Non-Static Block:

The block which is declared without static keyword is known as Non-Static Block. Unlike static, statements inside the Non-Static block will be executed whenever the object is created and before the constructor. If there are multiple in a class, then it will be executed in the order in which they are defined.

Syntax:  

{
           - - - - - - -- - - -
           - - -- - - -- - -- -
           - - - - - -- - - - -
}

Example:

class NonstaticTesting{
	 {
		System.out.println("Non-Static block 1");
	 }
	
	 NonstaticTesting ()
	 {
		 System.out.println("Constructor Block");
	 }
	public static void main(String args[]){
		
		System.out.println("Main Method");
		NonstaticTesting nst = new NonstaticTesting ();	
	}
	 {
		System.out.println("Non-Static Block 2");			
     }	
}

Output:

Main Method
Non-Static block 1
Non-Static Block 2
Constructor Block

Explanation:

From the above example, you can notice that the Non-Static block is executed before the constructor and after the object creation. That’s why “Main Method” is showing first in Output. Whenever the object reference is created, then only It will execute. After all Non-static blocks are executed it will execute the constructor.

Summary:

  • Block is a special type or component in java which placed only inside the class but outside the method. It cannot be placed inside the method. It starts with ‘ { ’ and ends with ‘ } ‘.
  • There are 2 types of block in java. Static Block and Non-Static Block.
  • Static block always executes before the main method. That means when you will start the execution the class, it always executes first static block and then it moves to main method.
  • The block which is declared without static keyword is known as Non-Static Block. Statements inside the Non-Static block will be executed whenever the object is created and before the constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *