Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
This article will introduce to another interesting topic that is Static Block In Java and will follow it up with a programmatic explanation. Following pointers will be covered in this article,
Java provides the user with a block known as static block, which is mainly used for the static initializations of a class. The block consists of a set of statements which are executed before the execution of the main method. This is due to the fact that the class has to be loaded into the main memory before its usage, and static block is executed during the loading of the class. On defining a number of static blocks in a program, the blocks execute from the top to the bottom.
Moving on with this article on Static Block in Java
Syntax:
static { ........ //Statements ........ }
Moving on with this article on Static Block in Java
Example of a Static Block
class Static { static int p; int q; // creating the static block static { p = 18; System.out.println("This is the static block!"); } // end of static block } public class Main { public static void main(String args[]) { // Accessing p without creating an object System.out.println(Static.p); } }
Output:
This is the static block!
18
It must be noted that static blocks are executed before constructors, as seen in the following example:
class Stat { static int p; int q; static { p = 18; System.out.println("This is a static block!"); } Stat(){ System.out.println("Constructor!"); } } public class Main { public static void main(String args[]) { // Even though we have two objects, static block is executed only once. Stat s1 = new Stat(); Stat s2 = new Stat(); } }
Output:
This is a static block!
Constructor!
Constructor!
Moving on with this article on Static Block in Java
Example of Multiple Static Blocks
We can also define multiple static blocks in a single program:
public class Stat { static { System.out.println("This is the first static block!"); } static { System.out.println("This is the second static block!"); } public static void main(String args[]) { System.out.println("Main!"); } }
Output:
This is the first static block!
This is the second static block!
Main!
These methods provide the users with an efficient way of using the static block.
Discover the power of Flutter and learn how to leverage its features in a Flutter Development Course.
Thus we have come to an end of this article on ‘Static Block In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co