Java SE 24th----static and final use trap keyword

Source: Internet
Author: User

1. For final type member variables, there are generally two ways to assign values;

A) The initial value is appended when declaring a member variable of the final type

 Package com.cl.staticandfinal;  Public class FinalTest4 {    finalint a=7;      Public FinalTest4 () {            }}

b) The initial value is not assigned when declaring a member variable of the final type, but it is assigned an initial value in all the constructor methods of the class


package com.cl.staticandfinal; Public class FinalTest4 { finalint A; Public FinalTest4 () { = 0; }}

2.static code block: Static code block. The function of static code blocks is also to do some initialization work. Execute the static code block first. Then the construction method is executed. Static blocks of code are executed when the class is loaded, and the constructor is executed when the object is generated, and to invoke a class to generate the object, you first need to load the class onto the Java Virtual Machine (JVM) and then the JVM loads the class to generate the object.

Package Com.cl.staticandfinal;public class StaticTest4 {public static void main (string[] args) {p p = new P (); P p1 = new P ();}} Class P{static{system.out.println ("static Block");} Public P () {System.out.println ("P");}}

Output Result:

Static block
P
P

Parsing: Static code blocks are executed when the class is loaded into the JVM, and the static code blocks are loaded once.

3. The static block of code for a class executes only once, when the class is loaded, because each class is only loaded once, so the static code block is only executed once, and the constructor does not, and the constructor of the class is called every time an object is generated, so the new one invokes the constructor method once.

4. If there are both construction methods and static code blocks in the inheritance system, then the static code block of the topmost class is executed, and the static code block is executed to the lowest level, and then the construction method of the topmost class is executed until the bottom-most construction method is executed. Note: static code blocks are executed only once.

 Packagecom.cl.staticandfinal; Public classStaticTest4 { Public Static voidMain (string[] args) {NewS (); NewS (); }}classp{Static{System.out.println ("P static Block"); }     PublicP () {System.out.println ("P Constructor"); }}classQextendsp{Static{System.out.println ("Q static Block"); }     PublicQ () {System.out.println ("Q Constructor"); }}classSextendsq{Static{System.out.println ("S static Block"); }     PublicS () {System.out.println ("S Constructor"); }}
Output Result:

P static block
Q static block
S static block
P Constructor
Q Constructor
S Constructor
P Constructor
Q Constructor
S Constructor

5.

Java SE 24th----static and final use trap keyword

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.