Java programming Idea--constructor initialization order __ algorithm

Source: Internet
Author: User

Within a class, the order in which variables are defined determines the order in which they are initialized. Even though variable definitions are scattered between method definitions, they are still initialized before any method, including the constructor, is invoked.

Package javastatic;
Class window{
	window (int marker) {
		System.out.println ("Windows (" +marker+) ");
	}
Class house{
	window w1=new window (1);
	House () {
		System.out.println ("house ()");
		W3=new Window (a);
	}
	Window W2=new window (2);
	void F () {
		System.out.println ("F ()");
	}
	Window W3=new window (3);
}

public class Orderofinitialization {

	/**
	 * @param args
	 *
	/public static void main (string[] args) { c22/>//TODO auto-generated method stubs House
		house=new House ();
		House.f ();
	}



Results

Windows (1)
Windows (2)
Windows (3)
House () Windows (33)
F ()

This shows that the first initialization of the variable, followed by the construction method, in addition, W3 this reference was initialized two times.

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.