[Webinar] Streamline your web hosting managementRegister Today


static block

Posted on 2000-02-09
Medium Priority
Last Modified: 2010-04-16

I wonder what would be the meaning of making a block of statements , that is not a method, static in a class.
If a method is static then it can be called without instanciating the object that contains that method, but what about a piece of code like this:

public class SomeClass implements Driver, PoolLog{
            SomeClass d = null;
                  d = new SomeClass();
            }catch(SQLException e){
                  d.log(e, "Unable to register driver with manager!");

      public SomeClass(){
            someVar = new Hashtable();


Question by:simi

Accepted Solution

jerch earned 80 total points
ID: 2504313
Static block is actually called static initialization block.  Basically, it is usually used in initializing static fields.  It is most useful when simple initialization clauses on the field declaration aren't up to the task of initialization. For example, creating a static array and initializing its elements sometimes must be done with executable statements.  Here is a small code that initializes a small array of primes.

class Primes {
protected static int[] primes = new int[4];

static {
primes[0] = 2;

for (int i = 1; i < primes.length. i++)
primes[i] = aStaticMethodCall();

Take note that you can only call static method.

PS. By the way SomeClass d would be useless since it will be out of scope. Of course you can declare variable for temporary use.

Author Comment

ID: 2504933

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question