The load order of inheritance relationships in Java

Source: Internet
Author: User


/* When loading a class, the Java virtual machine loads the parent class before loading the subclass, and initializes the static initialization block, static member variable (class variable), and static method. The object of the class is created only when the new method is called. Initializes a parent-child inheritance relationship by first executing the initialization block portion of the parental class, then constructing the parent class, executing the initialization block of the subclass, and finally constructing the subclass. The process of destroying an object is to first destroy the subclass part, and then destroy the parent class part. */public class Inheritanceloadorder {public static void main (string[] args) {System.out.println ("output without creating an object directly accessing a static method:"); Child.childmethod (); System.out.println ("\n\t>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>\n "); System.out.println ("When you create an object from new, access the output of a non-static method:"); new Child (""). Display (); System.GC ();//notifies the virtual machine to be garbage collected}}class parent{private int x = 15;private static int y = GetValue (20); { System.out.println ("Executes the initialization code block of the parent"); int x = 70;int y = getValue (30);} Static{system.out.println ("Execute static initialization code block of the parent"); int sx = 70;int sy = getValue (30);} Public parent () {System.out.println ("parent default constructor is called");} public void display () {System.out.println ("the display method of the parent is called"); System.out.println ("x =" + This.x + "; y = "+ y");p Arentmethod ();} public static void Parentmethod () {System.ouT.println ("The Parentmethod method of the parent is called");} public static int getValue (int num) {System.out.println ("Parent's getValue (int num) method is called"); return ++num;} When Java is garbage collected, the object's Finalize () method is called automatically protected void Finalize () {System.out.println ("The parent object is retracted by the garbage Collector");}} Class Child extends Parent{{system.out.println ("Execute child's initialization code block"); int z = 30;} static {System.out.println ("executes the statically initialized code block for Child");} Public Child () {super (); SYSTEM.OUT.PRINTLN ("Child's constructor method is called");} Public child (String str) {System.out.println ("the constructor method of the child parameter is called");} public static void Childmethod () {System.out.println ("The Childmethod method of child is called");} When Java is garbage collected, the object's Finalize () method is called automatically protected void Finalize () {System.out.println ("child object is retracted by the garbage Collector");}} /* Only when an object is created, the initialization block, construction method, and so on of the class are initialized in the order of the subclass after the Father's class. If you only access static methods of the class, the Java Virtual Machine does not initialize the code. For each class, the Java virtual machine is loaded only once, and at load time, the static methods, static variables, and static initialization blocks of the class are initialized. The System class's GC () method is used to notify the Java Virtual machine of garbage collection, but is not guaranteed to execute immediately, and when garbage collection is taken, it is entirely up to the virtual machine. The virtual opportunity invokes the Finalize () method of the object when it is being garbage collected. */



Operation Result:


The load order of inheritance relationships in Java

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.