Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Block

A Block is a pair of { } which we can declare inside a class directly or in any local context as well as inside a method or inside another block or inside a constructor.

Types of Blocks: -

1. Instance Block or Non-Static Block
2. Static Block
3. Local Block

1. Instance Block or Non-Static Block: -

• When we declare a block inside a class directly without static keyword it is called instance block or non-static block.
• An instance block executes at the time of its class object creation.
• In a class we can declare multiple instance blocks as per the business requirement.
• Sequence of instance blocks processing depends on the sequence of declaration.
• Inside the instance block we can access both instance and static member.
• Generally, we use an instance block to execute a set of codes at the time of its class object creation.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		A a2 = new A();
		A a3 = new A();
	}
}
class A{
	/* Instance Variable */
	int a = 10;
	/*Static Variable */
	static int b = 20;
	{
		System.out.println("IB-1 in A class");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
	}
	{
		System.out.println("IB-2 in A class");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
	}
} 
        
    IB-1 in A class
    a :- 10
    b :- 20
    IB-2 in A class
    a :- 10
    b :- 20
    IB-1 in A class
    a :- 10
    b :- 20
    IB-2 in A class
    a :- 10
    b :- 20
    IB-1 in A class
    a :- 10
    b :- 20
    IB-2 in A class
    a :- 10
    b :- 20
            

In the above program we have one instance variable and one static variable which are: int a = 10 and static int b = 20, and we have three instance blocks and inside every instance block we are accessing the value of int a = 10 and static int b = 20, which helps us to understand that we can access instance and static member both from an instance block of same class directly.

Note: - The sequence of memory allocation for instance variable and instance block depends on their sequence of declaration.

            
class JTC{
	public static void main(String arg[]){
		new A();
	}
}
class A{
	{
		System.out.println("IB-1 in A class");
		// System.out.println(a); error: illegal forward reference
	}
	int a = 10;
	{
		System.out.println("IB-2 in A class");
		System.out.println("a :- "+a);
	}
}
            
        
            IB-1 in A class
            IB-2 in A class
            a :- 10
            

In the above program we have a class A which contains two instance blocks. In the first instance block we are not able to access instance variable as we have declared instance variable after the first instance block but before second instance block, that is the reason we can access the value of a in the second instance block. Here the sequence of memory management of instance variable has been represented; and, as already mentioned earlier, sequence of instance block depends on the sequence of declaration.

2. Static Block: -

• When we declare a block inside a class directly with static keyword, it is called a static block.
• A static block executes at the time of its class loading.
• We can directly access only static member from the static block of the same class.
• In order to access an instance member from static block it is a prerequisite to create its class object.
• Static block executes at the time of its class loading. It means a static block executes once in its life cycle. Hence we use static block when we need to execute a set of codes, at the time of its class loading only.

            
class JTC{
	public static void main(String arg[]){
		new A();
	}
}
class A{
	int a = 10;
	static int b = 20;
	{
		System.out.println("IB in A class");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
	}
	static{
		System.out.println("SB in A class");
		/*System.out.println("a :- "+a);*/
		System.out.println("b :- "+b);
	}
	
}

            
        
    SB in A class
    b :- 20
    IB in A class
    a :- 10
    b :- 20        
            

In the above program we have a class A which has an instance variable and a static variable which are int a = 10 and static int b = 20 respectively, and an instance block and a static block as well. Here in the JTC class we are creating an object of A class and as we have discussed earlier, that first A.class file loads then its object is created. This is the reason that first the static block executes then the instance block is processed. The other concept which we are explaining here is that we can’t access instance member from static block directly, this is because all the static members work on early binding and instance members work on late binding concept.

Note: - The sequence of memory management of static variable and static block depends on the sequence of their declaration.

            
class JTC{
	public static void main(String arg[]){
		new A();
	}
}
class A{
	static{
		System.out.println("SB-1 in A class");
		/*System.out.println("a :- "+a); error: illegal forward reference*/
	}
	static int a = 10;
	static{
		System.out.println("SB-2 in A class");
		System.out.println("a :- "+a);
	}
}            
        
    SB-1 in A class
    SB-2 in A class
    a :- 10
            

In the above program we have a class which is A and inside the A class we have two static blocks and a static variable. Here the static variable has been declared after the first static block and before second static block, hence we are able to access static variable inside the second static block only. When we try to access static variable from first static block then we get an error as: illegal forward reference at compile time.

3. Local Block: -

• When we create a block inside a local context it is called a local block.
• A local block is not an instance block or static block .
• A local block executes when we invoke its local context.
• We use a local block in some special cases like to achieve block-level synchronization.

            
class JTC{
	public static void main(String arg[]){
		new A().m1();
	}
}
class A{
	void m1(){
		System.out.println("m1 method in A class");
		System.out.println("Before Local Block");
		{
			System.out.println("Local Block in m1 method");
		}
		System.out.println("After Local Block");
	}
}
        
        
    m1 method in A class
    Before Local Block
    Local Block in m1 method
    After Local Block
            

In the above program we have a class called A class, In A class we have an instance method m1 in which we have declared a local block.

            
class JTC{
	static{
		System.out.println("SB in JTC class");
	}
	public static void main(String arg[]){
		A a1 = new A();
	}
}
class A{
	int a = 10;
	static int b = 20;
	{
		System.out.println("IB in A class");
	}
	static{
		System.out.println("SB in A class");
	}
}

        
        
    SB in JTC class
    SB in A class
    IB in A class
            

In the above program we can see that we have a class JTC which contains the main method. Before invoking the main () class, the loader loads the JTC class and as we know that when a class loads then the static variable or static block gets its memory in JTC class. Here there is no static variable but there is a static block, so this will get executed first and thereafter JVM will invoke the main (). In main() because we are creating an object of A class, so first A class gets loaded by the class loader and static variable int a = 10 gets its memory, thereafter the static block executes and then the control moves to instance variable and instance block.

Dynamic Class Loading: -

In this tutorial we have seen different situations where a class can be loaded by the class loaders if it is not already loaded. In this section we will discuss about a built-in method called forName(String) that is basically used to load a class dynamically.

public static java.lang.Class<?> forName(java.lang.String) throws java.lang.ClassNotFoundException;

When we load a class using forName method then first it checks whether the specified class is already loaded into the JVM memory or not. If the class in not already loaded then only forName method loads the class. forName method returns an exception which is ClassNotFoundException when we try to load a class which is not available at the location.

            
class JTC{
	public static void main(String arg[]) throws ClassNotFoundException{
		Class.forName("A");
		Class.forName("B");
	}
}
class A{
	static{
		System.out.println("SB in A class");
	}
}
class B{
	static{
		System.out.println("SB in B class");
	}
}
        
        
    SB in A class
    SB in B class       
            

In this program we have a class JTC where we have a main() and in main method we use forName() to load class A and class B.