Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Implement Static Block In Java?

Published on Aug 16,2019 92 Views
51 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.