"0" README
0.1) This paper is transferred from http://blog.csdn.net/fykhlp/article/details/6236316;
the "1" text is as follows
The error is that the variable initialization is problematic and usually occurs in static variables, especially in the case of single mode. This problem is often caused by incorrect initialization order, and here's a simple example.
[Java] View Plain copy import java.util.hashmap; import java.util.map; public class example { private Static example example = new example (); private static Map<Integer,Boolean> test = new HashMap<Integer, Boolean> (); private example () { test.put (1, true); } public static example getinstance () { return example; } }
If you call getinstance in another class, you will get an error exceptionininitializererror. This is because a class does not assign a value to an instance variable when it is loaded, and the object is created without assigning a value to a static variable. When we call getinstance, this class starts loading, does not assign a value to the instance variable while loading, but assigns a value to the static variable in order, so assign the example first, then assign the value to the test to initialize. However, a minor episode occurred when assigning a value to example, which calls the constructor to create an object. When an object is created, it is not assigned a value for static variable test, and test is called inside the constructor, and the error is made.
To
[Java] view plain copy private static map<integer,boolean> test = new Hashmap<integer, Bool Ean> (); private static Example Example = new Example ();
It's OK.