For more information about the sequence of initialization variables of Basic Java constructor objects, see

Source: Internet
Author: User
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);A b = new B(8);System.out.println(A.aa + " " + B.bb);}}class A {int a = f();static int aa = sf();static {aa = 11;System.out.println("A kuai static " + aa);}{aa = 111;a = 1;System.out.println("A kuai " + a);}A() {System.out.println("A constract begin. " + a);}A(int r) {System.out.println("A constract begin. " + a);}int f() {System.out.println("A kuai f");return 2;}static int sf() {System.out.println("A kuai sf");return 22;}}class B extends A {static int bb = sfun();int b = fun();static {bb = 44;System.out.println("B kuai static " + bb);}{b = 4;System.out.println("B kuai " + b);}B() {System.out.println("B constract begin. " + b);}B(int r) {System.out.println("B constract begin." + b);}int fun() {System.out.println("B kuai fun");return 3;}static int sfun() {System.out.println("B kuai sfun");return 33;}}/*A kuai sfA kuai static 11B kuai sfunB kuai static 44A kuai fA kuai 1A constract begin. 1B kuai funB kuai 4B constract begin.4111 44*/

Initialization sequence:

Static variables of the parent class, static variables of the Child class, instance variables of the parent class, construction methods of the parent class, instance variables of the Child class, and construction methods of the Child class

Static variable initialization:

1 ). When defining, 2 ). Static blocks; the initialization order is in the order they are initialized in the Code;

Initialization of instance variables:

1 ). When defining, 2 ). Static block, 3 ). Constructor;

Among them, 1 and 2 are executed before 3), and 1 and 2 are executed in the order in the code.

Are defined first, open the memory, assign its initial value (null, 0, etc.), and finally assign values according to the value assignment statement, that is, the final value.

 

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);System.out.println(A.sf());}}class A {static A Aa = new A(2);static int aa = 11;int a = 1;static {System.out.println("static " + aa);}{System.out.println("A kuai " + a);}A() {sf();System.out.println("A constract begin. " + a + " " + aa + " " + " "+ sf());}A(int r) {sf();System.out.println("A constract begin. " + a + " " + aa + " " + " "+ sf());}int f() {System.out.println("A kuai f " + a);return a;}static int sf() {System.out.println("A kuai sf " + aa);return aa;}}/*A kuai 1A kuai sf 0A kuai sf 0A constract begin. 1 0  0static 11A kuai sf 1111 */

A Kuai 1

Why is it output? Why is the static variable 0, but the instance variable has a value!

Is there a conflict with the above description? Don't you understand? Thank you for your guidance! Thank you!

 

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.