Why can't a static inner class have static-decorated properties, but can have constants?

Source: Internet
Author: User

Such as:
public class innerclassdemo{
int x;
Class a{
static int a = 0;//is not legal to write this way.
static final int b=0;//It's legal to write this.

}
}

Java class loading order, first load the class, execute the static variable initialization, and then execute the object creation, if we want to execute the code in the variable int a initialization, then we must first execute loading the external class, then load the inner class, and finally initialize the static variable A, the problem is the loading of the inner class, We can think of the inner class as a non-static member of the outer class, its initialization must be done after the outer class object is created, the internal class must be completed after the instance, the Java virtual machine requires that all static variables must be completed before the object is created, which creates a contradiction.
(a bit around, hehe)
And the Java constant in the memory of the constant pool, its mechanism and variables are different, at compile time, load constants do not need to load the class, so there is no such contradiction.

Why can't a static inner class have static-decorated properties, but can have constants?

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.