Java (static) variables and (static) code block execution sequence, java Variables

Source: Internet
Author: User

Java (static) variables and (static) code block execution sequence, java Variables
This article discusses the execution sequence of (static) variables and (static) code blocks in Java. First, three classes are created: 1. Foo class for printing variables.

public class Foo {    public Foo(String word) {        System.out.println(word);    }}
2. Parent class
public class Parent {    static Foo FOO = new Foo("Parent's static parameter");        Foo foo = new Foo("Parent's parameter");        static {        System.out.println("Parent's static code block");    }        {        System.out.println("Parent's code block");    }        public Parent() {        System.out.println("Parent.Parent()");    }}
3. Child class
public class Child extends Parent{    static Foo FOO = new Foo("Child's static parameter");        Foo foo = new Foo("Child's parameter");        static {        System.out.println("Child's static code block");    }        {        System.out.println("Child's code block");    }        public Child() {        System.out.println("Child.Child()");    }}
Next, run the instance:
public class Run {    public static void main(String[] args) {        new Child();    }}
Print result:
Parent's static parameterParent's static code blockChild's static parameterChild's static code blockParent's parameterParent's code blockParent.Parent()Child's parameterChild's code blockChild.Child()
Summary

Static variables and static code blocks in Java are executed during class loading. when instantiating an object, declare and instantiate the variable before executing the constructor. If the subclass inherits the parent class, the static variables and static code blocks of the parent class are executed first, and then the static variables and static code blocks of the subclass are executed. Similarly, the non-static code blocks and constructors of the parent class and subclass are executed.

Note: (static) variables and (static) code blocks also have execution sequence, consistent with the Code Writing sequence. (Static) variables can be used in (static) code blocks, but the used (static) variables must be declared before (static) code blocks.

Finally, the following steps are provided:

1. Parent static variables and static code blocks (first declared and executed );

2. Subclass static variables and static code blocks (first declared and executed );

3. Variables and code blocks of the parent class (first declared and executed );

4. constructor of the parent class;

5. Subclass variables and code blocks (first declared and executed );

6. constructor of the subclass.

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.