Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does c++ static keyword do with braces?

I found this code somewhere, perlin noise generator, I think:

static {
    for(int i=0; i<512; i++) perm[i]=p[i & 255];
}

What does the static do there? it was spammed in a lot other places as well... The code was practically built with static{} everywhere. I lost the original code somewhere so thats the only thing I have, but it was like that code above: no variable declarations there, which why I dont get it.

like image 790
Rookie Avatar asked Dec 03 '22 05:12

Rookie


2 Answers

I think this is Java, not C++, which would mean it's a static initialization block.

like image 94
James McLaughlin Avatar answered Dec 09 '22 14:12

James McLaughlin


My guess is that it is in fact java code and java static block. Basically, the block that is executed more or less when the static variable would be initialized. (when the class is loaded, but actually I'm not ready to answer questions tagged java).

like image 38
Michael Krelin - hacker Avatar answered Dec 09 '22 15:12

Michael Krelin - hacker