A block that is created using the static keyword is called static block. The static block is used to initialize the static members of the class. Static block is not member of the class, and it cannot have return statement.
The static block can use only other static members of the class, it cannot access instance members of the class.
Static block is executed only once before the execution of the main method and before constructor of the class.
A class can have any number of static blocks and they are executed in the order, in which they are defined.
this keyword and super keyword cannot be used in the static block.
In java 1.6 and earlier the static block was executed even if class did not have main method, but after java 1.6 the static block is executed only if our class has the main method. The following example demonstrates the static block.
public class DemoStaticBlock
{ static
{ System.out.println("This is the static block1");//Will be executed first
}
public static void main(String[]args)
{ System.out.println("Inside the main function");//will be executed at last
}
}
#java #tarunsir #staticblock #coding
On this page of the site you can watch the video online static block in java in hindi with a duration of hours minute second in good quality, which was uploaded by the user Tarun Sir 09 August 2021, share the link with friends and acquaintances, this video has already been watched 225 times on youtube and it was liked by 14 viewers. Enjoy your viewing!