Java learning Summary

Source: Internet
Author: User

I have been confused about the sequence of execution of static code blocks and constructor functions. I read "java object-oriented programming" by Sun weiqin and explained it in the Basic java video of instructor bi, here we will use the following small program to give a brief description to deepen our understanding ....

Public class Fu {

Static {
System. out. print (1 );

}

Public Fu (){
System. out. print (2 );
}
}

Public class Zi extends Fu {

Static {
System. out. print ("");
}

Public Zi (){
System. out. print ("B ");
}
}

Public class demo {

Public static void main (String [] args ){
Fu fu = new Zi ();
Fu = new Zi ();
}
}

1.
When both the parent class and subclass have static code blocks and constructors, the execution sequence is as follows:
Parent static code block> subclass static code block when a Java Virtual Machine loads a class, the code is executed.

Parent class constructor> subclass Constructor (first father, then child)

For multi-level inheritance relationships, the parent class at the upper level is executed first, and then gradually decreases.

Summary: static execution takes precedence over subclass execution.
The static code block is executed when the JVM loads the class, and the static code block is executed only once.


2.

Before calling a method in a class, assign a value to the member variables in the class before executing the method body. If no specific value is assigned to the Code, the default value of the variable is assigned. The assignment order of member variables is in the order of forward and backward. If the Code has both direct assignment and constructor assignment, run www.2cto.com in sequence.

3.
Overload)
In a class definition, you can write several methods with the same name, but as long as their signature parameter list is different, Java regards them as the only method. Simply put, a method in a class has the same name as another method, but the parameter table is different. This method is called a overload method.

Override, subclass re-implements the method in the parent class;
1. The return values of the two methods in which method rewriting occurs. The method name and parameter list must be completely consistent.
2. The exception thrown by the subclass cannot exceed the exception thrown by the corresponding method of the parent class.
3. The access level of the subclass method cannot be lower than the access level of the corresponding method of the parent class (public, package, protected, private)

4.

Dynamic binding: In the runtime environment, when subclass B calls a series of instance methods (including one method to call another method ), dynamically bind the instance method contained in Class B. If Class B does not define this instance method, it is dynamically bound to the instance method inherited from the parent class.

After reading the above instructions, you should also know the output results of the above program.
Output result: 1a2b2b

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.