0

I've been following some tutorials to get used to libgdx while learning Java, and I found this piece of code in a class which intrigued me:

    static Map<Keys, Boolean> keys = new HashMap<Keys, Boolean>();
static {
    keys.put(Keys.LEFT, false);
    keys.put(Keys.RIGHT, false);
    keys.put(Keys.JUMP, false);
    keys.put(Keys.FIRE, false);
};

You can mostly ignore the functionality of this. Keys is an enum and everything else should be self explanatory. The weird thing is that this piece of code is outside any method at all, and actually that static {} block is being declared as some kind of method.

So, what's this? What does it do? Does it execute that piece of code when the class is created? Or when the program first runs? (it's static after all). I'd just like to know what this kind of (let's call it) method is, if it's got a name, and what restrictions I've got using this. I'd also like to ask for some efficiency, or even good practise advice about wether to use this or avoid it.

Thank you very much.

Setzer22
  • 1,589
  • 2
  • 13
  • 29

3 Answers3

2

So, what's this?

It's a static initialization block. See also 8.7 of the language specification.

What does it do?

It's used to initialize the class variables.

Does it execute that piece of code when the class is created?

When the class is initialized. For details on when that happens, see 12.4.1 of the language specification.

Or when the program first runs?

Not necessarily. It's done immediately before it's needed, which might not be until some other point in the program execution. Again, see the specification.

I'd just like to know what this kind of (let's call it) method is, if it's got a name, and what restrictions I've got using this.

It is a special method used for initializing class variables. It is called a "static initializer". The restrictions are covered pretty thoroughly in the specification.

I'd also like to ask for some efficiency, or even good practise advice about wether to use this or avoid it.

They are useful in certain situations when you need to initialize a class variable as in your sample code; I'd consider that a good use of a static initializer.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Thank you! Java has all this sort of small things I still don't know... And it really looked preety much like C++ in the beginning – Setzer22 Jul 17 '13 at 11:24
0

Those are static Blocks.

StaticInitializer:
    static Block

As per JLS-8.7

A static initializer declared in a class is executed when the class is initialized (§12.4.2). Together with any field initializers for class variables (§8.3.2), static initializers may be used to initialize the class variables of the class.

Note :

Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

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

It can be thought of as a "class constructor".

Note that there are also instance initializers, which look the same, except that they don't have the static keyword. Those are run in addition to the code in the constructor when a new instance of the object is created.

Answer taken from: here

Community
  • 1
  • 1
nook
  • 2,378
  • 5
  • 34
  • 54