5

I'm not a pro in OOP so please bear with me.

This piece of code is from android's auto generate Master/Detail Flow project :

static {
        // Add 3 sample items.
        addItem(new DummyItem("1", "Item 1"));
        addItem(new DummyItem("2", "Item 2"));
        addItem(new DummyItem("3", "Item 3"));
    }

What is that? I mean is it a method?a constructor?or what?

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • 1
    Its a keyword in Java, static is vast and it does not need an instance. I would suggest you Google it to get more information. – Skynet Sep 14 '13 at 05:26
  • 2
    It has highest priority for execution. If you want to execute any statements before main method write in static block. – Biraj Zalavadia Sep 14 '13 at 05:27
  • 1
    Thanks. I google something like static java, but all i get is a "static class", "static method", etc. – Blaze Tama Sep 14 '13 at 05:27
  • 2
    Static is a keyword you can use it with a class, a method, a variable and even a block. All hail to Java, its God's own language. – Skynet Sep 14 '13 at 05:28

3 Answers3

7

This is called static initializer.It's executed when the class is loaded (or initialized, to be precise, but you usually don't notice the difference).

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
4

This is referred to as a static block.

It will run at class loading time, the same time all static variables are loaded, so in a way you can think of this as a constructor for static content, because you don't have to instantiate a class for these operations to run. Or an hook for when the class is loaded.

Thihara
  • 7,031
  • 2
  • 29
  • 56
0

It is called as static block..It is executed before main method at the time of class loading..It is used to initialize the static data members...By using static block we can execute the program without the main method....

Revathi Bala
  • 159
  • 2
  • 2