1

Can anyone suggest me whats the difference between Line 1 and LINE 2.Which is the preferred way?If we can create an object ,then why do we use static block? what is the order of execution of static members in a class?

class Foo {

static {
    // bar = new Bar();
    System.out.println("IN Block");//LINE 1
}
public static Bar bar = new Bar();//LINE 2

public void doStuff() {
    System.out.println("I am in Foo");

}
}

Thanks

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
user3062513
  • 410
  • 1
  • 10
  • 19
  • "*.. what is the difference between Line 1 and LINE 2*" Both lines are doing entire different things, can you describe what confuses you? "*Which is the preferred way*" of doing what? – Pshemo Apr 25 '14 at 13:20
  • 2
    one is printing something, the other assigns a variable. – Joffrey Apr 25 '14 at 13:21

5 Answers5

0

The static block will be executed when the class is loaded by the JVM. There's no reason to use it to initialize static fields except in perhaps certain edge cases.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

The first is a static anonymous block, its gets called once by the class loader. When the Foo is first referenced.

The second is a static variable, one instance of bar is shared between all foos.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

In your example, there isn't that much difference between the two lines. You've created a static instance, but what if you need to do more with your static instance, like load your Bar with some functionality?

static {
    bar = new Bar();

    bar.doSomeStuff("This", "Is", "An", "Argument", "List");
}

That is the purpose of a static block. As for when they are executed, they are ran when the class is loaded by the JVM, much in the same way as static fields are created.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code. And dont forget, this code will be executed when JVM loads the class. JVM combines all these blocks into one single static block and then executes. Here are a couple of points I like to mention:

If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.


If you’re referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM 
i.e., now the static variables/methods referred and the static block both will be executed.
niiraj874u
  • 2,180
  • 1
  • 12
  • 19
0

As many of us replied already, a static block is executed when the class is loaded.

Now creating the object, object creation has no relationship with static block. Let's take a scenario, some times you need some code that should be executed before the actual instance is created. E.g.

public class Test{
     static{ 
        //Load Library 
        System.loadLibrary("hello"); 
     }

     void aMethod(){ 
       //some code
     }

     public static void main(String str[]){
         //some code for main
     }
}

when you use:

>java Test

So the main intention here is before you call any method or even the class is initialized, it should load the library to the system environment. This library may be used anytime anywhere through out the program. This is a good example of using static block. It is not really used just for statements such as System.out etc.

Any questions/confustion? please let me know?

santu
  • 665
  • 2
  • 7
  • 23