The initialization process of Java learning objects

Source: Internet
Author: User
Tags first row

When an object is created, the object initializes the procedure:

1, the implicit initialization of the data members in the class. (Num is implicitly initialized to 0 in the following figure example)

2, the subclass of the constructor into the stack.

3, the parent class constructor is initialized. (Because the first row of the subclass constructor has an implicit super ())

4, the subclass of the member variable display initialization. (Num is initialized to 9 in the following figure example)

5, the construction of code block display initialization.



Class Fu
{
	fu ()
	{
		System.out.println ("Fu constructor Run ...");
		Show ();
	}
	void Show ()
	{
		System.out.println ("Fu show Run");
	}
Class Zi extends Fu
{
	int num = 9;
	{
		System.out.println ("constructor code ..." + num);
		num = ten;
	}
	Zi () 
	{
		System.out.println ("Zi constructor run ..." + num);
	}
	void Show ()
	{
		System.out.println ("Zi show run ..." + num);
	}
public class Classtest 
{public
	static void Main (string[] args)
	{
		new Zi ();
	}
}
/* Run output
Fu constructor run ...
Zi Show Run ... 0
Constructor Code ... 9
Zi Constructor run ... Ten
* *


The parent class constructor calls show () in a subclass because the child class object is currently established, and this pointer points to the current subclass object.

The show () method of the parent class object is overwritten by the show () method of the Quilt class object, so now find the show () method in the Subclass object.

If not, find the show () method in the parent class object.

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.