static block

Hi,

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{
      static{
            SomeClass d = null;
            try{
                  d = new SomeClass();
                  DriverManager.registerDriver(d);
            }catch(SQLException e){
                  d.log(e, "Unable to register driver with manager!");
            }
      }

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

......

}
simiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jerchCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simiAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.