static-initializer

Java enum reverse look-up best practice

霸气de小男生 提交于 2019-11-27 06:43:07
I saw it suggested on a blog that the following was a reasonable way to do a "reverse-lookup" using the getCode(int) in a Java enum: public enum Status { WAITING(0), READY(1), SKIPPED(-1), COMPLETED(5); private static final Map<Integer,Status> lookup = new HashMap<Integer,Status>(); static { for(Status s : EnumSet.allOf(Status.class)) lookup.put(s.getCode(), s); } private int code; private Status(int code) { this.code = code; } public int getCode() { return code; } public static Status get(int code) { return lookup.get(code); } } To me, the static map and the static initializer both look like

Program hangs if thread is created in static initializer block

不打扰是莪最后的温柔 提交于 2019-11-27 01:08:04
I have come across a situation where my program hangs, looks like deadlock. But I tried figuring it out with jconsole and visualvm, but they didn't detect any deadlock. Sample code: public class StaticInitializer { private static int state = 10; static { Thread t1 = new Thread(new Runnable() { @Override public void run() { state = 11; System.out.println("Exit Thread"); } }); t1.start(); try { t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("exiting static block"); } public static void main(String...strings) { System.out

Initialization Order of Static Fields in Static Class

爱⌒轻易说出口 提交于 2019-11-26 18:51:45
given the following code: public static class Helpers { private static Char[] myChars = new Char[] {'a', 'b'}; private static Int32 myCharsSize = myChars.Length; } Is it guaranteed that myChars will be initialized before I use its length to assign to myCharsSize ? Yes, they will, please see 10.4.5.1 Static field initialization : The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (Section 10.11) exists in the class, execution of the static field

How to force a class to be initialised? [closed]

删除回忆录丶 提交于 2019-11-26 17:48:40
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . What is the best and cleanest way to do this? Specifically, I need some code in a static initializer block to run in that class, but I'd like to make this as clean-looking as possible. 回答1: Loading !=

Java enum reverse look-up best practice

蹲街弑〆低调 提交于 2019-11-26 17:35:37
问题 I saw it suggested on a blog that the following was a reasonable way to do a "reverse-lookup" using the getCode(int) in a Java enum: public enum Status { WAITING(0), READY(1), SKIPPED(-1), COMPLETED(5); private static final Map<Integer,Status> lookup = new HashMap<Integer,Status>(); static { for(Status s : EnumSet.allOf(Status.class)) lookup.put(s.getCode(), s); } private int code; private Status(int code) { this.code = code; } public int getCode() { return code; } public static Status get

How do you disable lazy class loading/initialization in Sun's JVM?

强颜欢笑 提交于 2019-11-26 16:53:33
问题 By default, Sun's JVM both lazily loads classes and lazily initializes (i.e. calls their <clinit> methods) them. Consider the following class, ClinitBomb , which throws an Exception during a static{} block. public class ClinitBomb { static { explode(); } private static void explode() { throw new RuntimeException("boom!"); } } Now, consider how to trigger the bomb: public class Main { public static void main(String[] args) { System.out.println("A"); try { Class.forName("ClinitBomb"); } catch

Are Java static initializers thread safe?

你说的曾经没有我的故事 提交于 2019-11-26 12:03:14
I'm using a static code block to initialize some controllers in a registry I have. My question is therefore, can I guarantee that this static code block will only absolutely be called once when the class is first loaded? I understand I cannot guarantee when this code block will be called, I'm guessing its when the Classloader first loads it. I realize I could synchronize on the class in the static code block, but my guess is this is actually what happens anyway? Simple code example would be; class FooRegistry { static { //this code must only ever be called once addController(new

Why doesn&#39;t Java allow to throw a checked exception from static initialization block?

筅森魡賤 提交于 2019-11-26 11:14:50
Why doesn't Java allow to throw a checked exception from a static initialization block? What was the reason behind this design decision? Kosi2801 Because it is not possible to handle these checked exceptions in your source. You do not have any control over the initialization process and static{} blocks cannot be called from your source so that you could surround them with try-catch. Because you cannot handle any error indicated by a checked exception, it was decided to disallow throwing of checked exceptions static blocks. The static block must not throw checked exceptions but still allows

Program hangs if thread is created in static initializer block

我的梦境 提交于 2019-11-26 09:37:26
问题 I have come across a situation where my program hangs, looks like deadlock. But I tried figuring it out with jconsole and visualvm, but they didn\'t detect any deadlock. Sample code: public class StaticInitializer { private static int state = 10; static { Thread t1 = new Thread(new Runnable() { @Override public void run() { state = 11; System.out.println(\"Exit Thread\"); } }); t1.start(); try { t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace

Initialization Order of Static Fields in Static Class

北城以北 提交于 2019-11-26 06:37:46
问题 given the following code: public static class Helpers { private static Char[] myChars = new Char[] {\'a\', \'b\'}; private static Int32 myCharsSize = myChars.Length; } Is it guaranteed that myChars will be initialized before I use its length to assign to myCharsSize ? 回答1: Yes, they will, please see 10.4.5.1 Static field initialization: The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the