JAVA basics-differences between final and static and class loading sequence

Source: Internet
Author: User

 

I. Summary of final and static in JAVA

 

 

Final

Static

Modifier

This class cannot be inherited

Only internal classes can be modified. This class does not require new and is statically loaded (nested top-level classes)

Modifier Interface

×

×

Modifier Constructor

×

×

Modify statement Block

×

The Virtual Machine initializes the static member field and the static statement block in the declared order.

Modify Field attributes

Attribute cannot be modified after initialization (instance constant)

Is a class variable. All instances share the variable value (class variable)

Assign values in definitions or Constructors (each instance has a copy)

Modifier

This method cannot be overwritten (only inherited)

Yesclass method. All instances share this method (class method)

Modify function parameters

Parameter values cannot be modified by functions

×

Modifier local variable

Local variables cannot be modified after initialization (local constants)

×

 

 

 

Purpose

1). To prevent methods from being overwritten or rewritten;

1) define global variables or constants;

2). Improve the running efficiency. JAVA uses an embedded mechanism for final method calls;

2). Define class methods;

 

 

 

Ii. JAVA class loading sequence

JAVAClass loading sequence:

1) recursively Load Static members/code blocks in the order of code, first parent class and then this class;

 

2) recursively load non-static members/code blocks in the order of code, first parent class and then this class;

 

3) The constructor is called recursively in the order of code, and the parent class is added to the class;

 

 

JAVAClass addition time:

1) when a static member is called, the class and parent class of the static member are loaded;

 

2). Load the new object for the first time (the new object will not be loaded for the second time)

 

3). The parent class will be loaded before the subclass is loaded. Members with static and final modifiers will not be loaded and used as constants;

This article is from the "when you start watching the cloud" blog

 

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.