No, it’s fine.
Cas
No, it’s fine.
Cas
So what it does is, like variables, it just executes the code right away when the class is first loaded somewhere somehow. It’s actually pretty cool. Its like an initialize without having to call it
O.o That’s probably one of the most useful things I’ve learned yet. Necro posts are awesome.
‘aaahahhhhhH!!!’ STATIC aaaahhh
It’s probably useful to do stuff, which is near the machine, but I really hate it actually… In my opinion nothing really needs to be static, unless it’s close to the machine…
I was actually just about to edit my post to ask if there was another way to do it without using static{}.
That particular static initializer is also completely pointless since it’s exactly the same as saying:
public class Test {
private static String value = "test";
public static String getValue() {
return value;
}
}
However, they do come in handy later: http://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
Namely, you can have the holder execute more or less arbitrary initialization code in its static initializer. I use that trick here: https://bitbucket.org/chuck/jcod/src/c2e8ae6fd81bafc50bce94e2b2c9a0a26f3137d6/src/main/java/net/fishbulb/jcod/display/TileDisplay.java?at=default#cl-150
[quote]Wont this code give a compile error? I’ve never seen code that just says “static{}” and does stuff within that.
[/quote]
This also works, can be run without a main method.
public class StartStatic
{
static
{
System.out.println("Hello, main is overrated...");
}
}
or even
public class StartStatic
{
static
{
System.out.println("Dont like main");
new Thread()
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Im running! "+i);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}.start();
}
}
Would be a good way to make your teacher mad in a programming class.
A good example of when to use the static initialization is Riven’s implementation of sin/cos using lookup tables: here
private static final float RAD,DEG;
private static final int SIN_BITS,SIN_MASK,SIN_COUNT;
private static final float radFull,radToIndex;
private static final float degFull,degToIndex;
private static final float[] sin, cos;
static
{
RAD = (float) Math.PI / 180.0f;
DEG = 180.0f / (float) Math.PI;
SIN_BITS = 12;
SIN_MASK = ~(-1 << SIN_BITS);
SIN_COUNT = SIN_MASK + 1;
radFull = (float) (Math.PI * 2.0);
degFull = (float) (360.0);
radToIndex = SIN_COUNT / radFull;
degToIndex = SIN_COUNT / degFull;
sin = new float[SIN_COUNT];
cos = new float[SIN_COUNT];
for (int i = 0; i < SIN_COUNT; i++)
{
sin[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * radFull);
cos[i] = (float) Math.cos((i + 0.5f) / SIN_COUNT * radFull);
}
}
In other words, when you need complex static data structures that can be generated procedurally.
Have you noticed who made the first reply to the thread? :
A good example of when to use the static initialization is Riven’s implementation of sin/cos using lookup tables: here
private static final float RAD,DEG;
private static final int SIN_BITS,SIN_MASK,SIN_COUNT;
private static final float radFull,radToIndex;
private static final float degFull,degToIndex;
private static final float[] sin, cos;
static
{
RAD = (float) Math.PI / 180.0f;
DEG = 180.0f / (float) Math.PI;
SIN_BITS = 12;
SIN_MASK = ~(-1 << SIN_BITS);
SIN_COUNT = SIN_MASK + 1;
radFull = (float) (Math.PI * 2.0);
degFull = (float) (360.0);
radToIndex = SIN_COUNT / radFull;
degToIndex = SIN_COUNT / degFull;
sin = new float[SIN_COUNT];
cos = new float[SIN_COUNT];
for (int i = 0; i < SIN_COUNT; i++)
{
sin[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * radFull);
cos[i] = (float) Math.cos((i + 0.5f) / SIN_COUNT * radFull);
}
}
In other words, when you need complex static data structures that can be generated procedurally.
Have you noticed who made the first reply to the thread? :
No one is going to win this argument. Some like to use singletons while others like to use statics. I kind of like to mix it up depending on mostly my mood and not from a coding standpoint.
I have a friend at EA UK who told me that they don’t care whether you use singletons or static globals (namespaced as this was in c++) as long as your sibgletons are not “lazy” (for memory efficiency obviously).
No one is going to win this argument. Some like to use singletons while others like to use statics. I kind of like to mix it up depending on mostly my mood and not from a coding standpoint.
I have a friend at EA UK who told me that they don’t care whether you use singletons or static globals (namespaced as this was in c++) as long as your sibgletons are not “lazy” (for memory efficiency obviously).