The Java compiler's compromise on null final assignment

Source: Internet
Author: User

The code is as follows:

public class Example038 {private final static string guest_id = "Ape_it";p rivate final static string Id;static {try {ID = GetID ();} catch (Getidexception e) {ID = Guest_id;e.printstacktrace ();}} private static String GetID () throws getidexception {throw new Getidexception ();} Class Getidexception extends Exception {}}


Code Analysis:

As the code above, there will be compile error reminders when editing in Eclipse (ID fields cannot be assigned repeatedly). The ID field is an empty final field and can only be assigned once. From the code, the ID is actually only assigned once, but why is there such a reminder?

It is a difficult problem to determine whether a program can assign a null final to more than one time. In fact, this is not possible. This is equivalent to classical downtime (like the paradox of God and the Barber paradox), which is often considered impossible to solve. To be able to write a compiler, the language specification takes a conservative approach at this point. In a program, an empty final field can be assigned only if it is definitely not assigned a value. Because it is conservative, the compiler must reject certain programs that can prove to be safe. The code above can prove to be a safe code, but the compiler must reject it because it cannot prove that only one time is assigned to the null final field.

The solution to the above-mentioned errors due to compiler compromise and conservatism is only to modify the code, and one way to modify the code is to change the empty final domain into a normal final domain, as follows:

public class example038 {public final static string guest_id =  " Ape_it ";// private final static string id;private final static string  id = getidnew ();//static {//try {//id = getid ();//} catch  ( Getidexception e)  {//ID = GUEST_ID;//                       e.printstacktrace ();//}//} Private static string getidnew ()  {try {return getid ();}  catch  (Exception e)  {return guest_id;}} Private static string getid ()  throws GetIDException {throw new  Getidexception ();} static class getidexception extends exception {private static final  long serialversionuid = 1l;}}


Note: This "Java doubts" series are bloggers read the original book "Java Doubts" after the original book on the explanation and examples of the adaptation and then write a blog post. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

The Java compiler's compromise on null final assignment

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.