Static Blocks in Java: Exception & Examples

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

In this lesson, we'll take a look at a block in Java, explain a static block and show some examples. We will then learn what about exceptions and whether you can have a static block exception.

Getting Started

Initialization is a part of every programmer's life. As you know, this is assigning the very first value to some variable within your program. If you think about it, initialization happens in the real-world as well. Examples include signing the back of your credit card, entering the password for a new social media account, or putting quarters in the machine to start the manual car wash. It's a fact of life. Many things require something to get them started. The Java programming language is no different. Variables need initial values, methods need input, and loops sometimes need counters. Java accomplishes this in a couple of ways. One of them is with the static block.

What is a Block in Java?

A block in Java is a collection of zero or more program code statements, enclosed in curly braces {}, that perform a specific task. The purpose of a block is to allow a number of statements to be executed where a single statement normally would. For example, consider the following:


if(condition) {
 // start of block 1
  statement1;
  statement2;
// end of block 1
} else {
  // start of block 2
  statement1;
  statement2;
//end of block 2
}


In this case, the true and false clauses of an 'if' statement are replaced with a block. Statements of this form are quite common in Java programs. More examples you may be familiar with can be found in the statement bodies of various looping constructs such as 'while', 'for', and 'do while'.

Static Blocks in Java

A static block is a special type of block that has the keyword 'static' preceding it. For example:


static {
  statement1;
  statement2;
}


This declares a static block with two statements in it. The purpose of a static block is to perform the static initializations for a class. Static blocks are executed only once, when the class is first instantiated, and are typically used to set the initial values for static variables. An interesting feature of static blocks is that you can have more than one of them. In this case, they are executed in the order they are written in the code.

Exceptions

In simple terms, an exception is an error, a run-time event that causes a program to deviate from its normal execution steps. For example, dividing a value by zero will cause an exception. When this type of event occurs, execution is turned over to the run-time system for processing. Java catches many types of exceptions and creates an exception object that contains various pieces of information that can be used to further process the exception. It is up to the programmer to anticipate and create the proper responses to exceptions. These are collectively known as handlers.

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