Analysis of loading sequence of classes in Java (commonly used in interview questions) _java

Source: Internet
Author: User

This is actually a last year's school recruit I met an Alibaba pen question (admits a little long time-. --Well, if I remember correctly, it was a big question in the Java direction. Of course, the question is not so straightforward, the topic only requires you to write the program after the operation of all System.out.println output results, where the program is the topic to, and the different execution order of each System.out.println will result in the final program output is different.

I'm sure I can't remember the exact subject, but we can change the question directly. If Class A and class B have static variables, static statement blocks, non-static variables, non-static statement blocks, constructors, static methods, Non-static methods, and Class A inherits Class B, what is the order of loading within the class when instantiating a?

At that time I was also confused, afterwards I wrote a small demo, this just know the class inside the actual load shun, test code as follows:

Class B:

public class b{
//static variable static	
int i=1;
Static statement block
static {
	System.out.println ("Class b1:static blocks" +i);
}
Non-static variable
int j=1;
Static statement block
static{
	i++;
	System.out.println ("Class b2:static blocks" +i);
}
Constructor public
B () {
	i++;
	j + +;
	System.out.println ("constructor B:" + "i=" +i+ ", j=" +j);
}
Non-static statement block
{
  i++;
  j + +;
  System.out.println ("Class b:common blocks" + "i=" +i+ ", j=" +j);
}
Non-static method public
void Bdisplay () {
	i++;
	System.out.println ("Class b:static void Bdisplay ():	" + "i=" +i+ ", j=" +j);
	return;
}
static method public
static void Btest () {
	i++;
	System.out.println ("Class b:static void Btest ():	" + "i=" +i);
	return;
}


Class A:

public class A extends b{
//static variable static	
int i=1;
Static statement block
static {
	System.out.println ("Class a1:static blocks" +i);
}
Non-static variable
int j=1;
Static statement block
static{
	i++;
	System.out.println ("Class a2:static blocks" +i);
}
Constructor public
A () {
  	super ();
	i++;
	j + +;
	System.out.println ("constructor A:" + "i=" +i+ ", j=" +j);
}
Non-static statement block
{
  i++;
  j + +;
  System.out.println ("Class a:common blocks" + "i=" +i+ ", j=" +j);
}
Non-static method public
void Adisplay () {
	i++;
	System.out.println ("Class a:static void Adisplay ():	" + "i=" +i+ ", j=" +j);
	return;
}
static method public
static void Atest () {
	i++;
	System.out.println ("Class a:static void Atest ():	" + "i=" +i);
	return;
}

Class classloading:

public class Classloading {public
	
	static void Main (String args[]) {
		a a=new a ();
		A.adisplay ();
	}

The results of the program operation are shown as follows:

Through the above diagram, we can see the whole loading process of Java class clearly.

1. To load Class A, static variables that execute their parent class B (Object) and static statement blocks (sequential order of execution) are loaded first.
2. Then load the static variables of execution Class A and the static statement block. (and 1, 2 steps will only be performed 1 times)
3. If you want to instantiate Class A, the constructor of its parent class B is called first, and a non-static variable and a non-static statement block in parent Class B is called first, before the constructor of its parent class B is called. Finally, the constructor initialization in parent class B is called.
4. Then call the non-static variable and the non-static statement block in Class A in turn. The last call to initialize the constructor in a. (and 3, 4 steps can be repeated)
5. Both static and Non-static methods are passive invocations, that is, the system does not invoke execution automatically, so the user does not execute without calling, mainly because the static method can be directly invoked directly with the class name (the instantiated object is also possible), and the Non-static method can only be invoked before the object is instantiated.

OK, today summed up here, if there is a bad or wrong place to say, you are welcome to point out, will be corrected, thank you.

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.