The cause of Java.lang.ExceptionInInitializerError

Source: Internet
Author: User

"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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.