Why Java cannot have static-decorated properties in a non-static inner class, but can have constants? __ "03" Java

Source: Internet
Author: User

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

}
}

Java class load order, the class is loaded first, the static variable is initialized, and then the object is created, and if we want to execute the variable int a initialization in the code, we must first load the outer class, then load the inner class, and finally initialize the static variable A, and the problem is to load the inner class. We can think of the inner class as a non-static member of the outer class, which must be initialized after the outer class object has been created, and that the internal class must be completed after the instance is externalized, and that the Java Virtual machine requires that all static variables 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, compile, load constants are not required to load the class, so there is no contradiction above.

Related Article

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.