Static Block vs. Constructor in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Static Blocks in Java: Exception & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Static Block vs Constructor
  • 1:29 Using Static Blocks &…
  • 2:23 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

This lesson will discuss the features of static blocks and constructors in Java. Working code examples will be provided to highlight each of these lines of code.

Static Block vs. Constructor

Static Block

In a Java class, a static block is a set of instructions that is run only once when a class is loaded into memory. A static block is also called a static initialization block. This is because it is an option for initializing or setting up the class at run-time. The keyword 'static' indicates that it spans all instances of the class. It is like a mini-global set of instructions.

Let's take a look at how to declare a class and a static block.


public class SvC {
static {
  //static block
  System.out.println("Hi, I'm a Static Block!");
  }
}


Constructor

On the other hand, constructors are run EACH time a new instance of a class is created. You can overload a constructor, meaning that multiple versions of the constructor can be created. Each one has to have a different number of parameters, but, otherwise, they can all be named the same. Let's look at some examples. We'll add the constructor after the static block statements.


public class SvC {
static {
  //static block
   System.out.println("Hi, I'm a Static Block!");
  }
  public SvC() {
  System.out.println("Constructor 1 Says Hi!");
  }
  public SvC(int c) {
   System.out.println("Constructor 2 Says Hi!");
  }
}


Finally, when we create an instance of the class in the main program, the given constructor is called. However, the static block only loads once in memory. Let's test it out. The following code creates instances of the SvC class.


public static void main(String[] args) {
  int counter = 15;
  SvC object1 = new SvC();
  SvC object2 = new SvC();
  SvC object3 = new SvC();
  SvC object4 = new SvC();
  SvC object5 = new SvC();
}


When the code is run, the following output is displayed.


Java static block constructor output


Since the static block is always executed first, it doesn't matter in which order it's placed within the class. Typically, you would want to have the static block listed before constructors for readability, but Java technically does not care. However, Java will process the static block codes in order.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support