Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java polymorphic methods inside constructors complete analysis     - Automatic and Manual Proxy Settings Switch GNOME Shell Extension Proxy Switcher Recommend (Linux)

- MySQL multi-instance configuration (Database)

- C ++ two second pointer memory model (two-dimensional array) (Programming)

- Getting Started with Linux system to learn: how to check the version of SSH on Linux (Linux)

- Will Laravel become the most successful PHP framework? (Programming)

- OpenSSH version smooth upgrade method (Linux)

- PF_RING 6.0.2 compiled and installed on Redhat 6.3 x86_64 (Linux)

- CentOS 7 hard disk expansion toss record (Linux)

- Ubuntu 14.04 compile RT288x_SDK / toolchain / squashfs3.2-r2 Error (Linux)

- pdf.js Tutorial (Linux)

- Qt for file splitting and fusion gadgets (Programming)

- Configuring automatic mail GAMIT under CentOS system (Linux)

- Java uses JDBC connect database (Programming)

- Django url () function Detailed (Programming)

- Linux command Detailed chpasswd bulk edit user password (Linux)

- MongoDB replication instance (Database)

- Using Java arrays implement sequential stack (Programming)

- mysqldump implement database logical backup (Database)

- Android Studio installed in Ubuntu 15.04 (Linux)

- Oracle users to automatically increase the partition table (Database)

  Java polymorphic methods inside constructors complete analysis
  Add Date : 2016-11-15      
  Let's look at an example, if you read "Java programming ideas", then there should be the impression.

package com.test.zj;

public class PolyConstructors {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        new RoundGlyph (5);


class RoundGlyph extends Glyph {
    private int radius = 1;

    public RoundGlyph (int r) {
        // TODO Auto-generated constructor stub
        radius = r;
        System.out.println ( "RoundGlyph radius ==" + radius);

    void draw () {
        // TODO Auto-generated method stub
        System.out.println ( "RoundGlyph draw () radius ==" + radius);


class Glyph {
    void draw () {
        System.out.println ( "print glyph.draw ()");

    Glyph () {
        System.out.println ( "Glyph () before draw ()");
        draw ();
        System.out.println ( "Glyph () after draw ()");



For students who are generally based java Here you might think that the output is as follows:

1 Glyph () before draw ()
2 RoundGlyph draw () radius == 1
3 Glyph () after draw ()
4 RoundGlyph radius == 5

But in fact, after you finished running, you will find that his output is this:

Some people may read here still do not understand what I want to express, I write a simple example. Define a parent SuperClass

package com.test.zj;

public class SuperClass
    private int superValue;

    public SuperClass ()
        setSuperValue (100);


    public void setSuperValue (int x)
        superValue = x;


Then we define a subclass of it:

// The subclass inherits from the parent class superclass
public class SubClass extends SuperClass
    private int subValue = 10;

    public SubClass ()

    // This method overrides the parent class
    public void setSuperValue (int x)
        // First call the parent class method
        super.setSuperValue (x);
        // Then we assign a value to its own variables
        subValue = x;


    public void printSubValue ()
        System.out.println ( "subclass subvalue ==" + subValue);


Finally, write a main function can be

package com.test.zj;

public class MainClass {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        SubClass sc = new SubClass ();
        sc.printSubValue ();


Well, now I believe many people will think that the results of the second example of the output should be 100

But in fact there is nothing eggs with his actual results are:

Both of these examples that in the end what happened, we look at the direct bytecode Well, this certainly will not be wrong bytecode, jvm bytecode how to write on how to perform.

We take a look at the first example.

It should be obvious to see our main function is to start a new RoundGlyph this object. Then we look at the results this class -c bars

We can see the constructor of the class

The first implementation is this:

That is the first implementation of the assignment constructor glyph then When the constructor glyph completed before execution

Our radius as an int variable before being executed automatically initialized jvm his is 0!

So here you should be able to guess a vague about the constructor glyph executing first, and then give yourself a member variable radius assignment.

Then we look at the glyph have done it:

You see constructor glyph in the middle of the time 13: invokevirtual # 31 here, to execute the draw method, but subclasses we rewrite this draw method

So you see the draw for subclasses to call the constructor in the glyph subclass radius when the assignment has not been executed, so this method subclass

The output value is zero, of course!

When the constructor of the parent class glyph after the execution of our assignments subclass finally be implemented. So here you should be able to understand the first example.

That we can now look to the second example, we are in fact very much the same. We still look at the second example manclass and main functions

Look here first new main function of a subclass of the subclass of it. Then of course we will take a look subclass init method

In fact, this place is a Subclass constructor.

Here you can see very clearly superclass constructor in the constructor subclass of where we are first performed, and then assigned to their subValue 10.

Then we went to see what has been done in the superclass. But in fact we have been able to come here think no matter what you do in the superclass when you finish later

SubValue values are bound to 10.

So when you subclass after object construction is completed at this time his subvalue member variable value is 10, so of course you print out the value of this variable is necessarily 10 a.

Of course, for a little clearer I put superclass constructor little talk about what has been done, although there is what has been done will not affect our conclusions, but still talk about it,

Even if it does not use any eggs. . .

You see this is a moment to call this method setSuperValue it, right, because subclasses override this method so we certainly have to look at the sub-category

This method of doing it:

Can not you see that they call setSupervalue parent class do, then subsequent calls to see you have a iload putfield

These two operations are not that give us a subclass subvalue assignment for you, right. Been here, our object constructor subclass first step:

The constructor calls the parent class even completed, and finally completed after the implementation of his assignment:

Well, these two examples here, even if the analysis is completed.

In fact, the final conclusion is that java programming ideas in to say:

Parent class static member -> subclass static member -> ordinary members of the parent class initialization and initialization block -> parent class constructor -> subclass ordinary member initialization and initialization block -> subclass constructors

If you are interested, you can write a little bit more complex procedures, verify the above conclusion is established, nonsense. . . . This conclusion is certainly valid. but

If you use this command to see them javap -c bytecode deeper, then I believe you can understand it!

Finally, say one more thing, we usually write code, try to avoid such wording above two examples, because this situation caused by the bug difficult to find. . . which is:

Try not to operate subclass member variables in the constructor of the parent class inside. If we want to initialize write very much trouble, consider using the initialization block such a clear way!

Do not ask me why this research, because tmd there is a bug is discovered for a long time for this reason ah! So after you find someone so written, please write direct mail copy to the whole group of complaints.
- SolrCloud-5.2.1 cluster deployment and testing (Server)
- Oracle creates split and splitstr functions (Database)
- To install Scribus 1.4.4 under ubuntu (Linux)
- Configuring Proxy on a Unix terminal, accelerate Android Studio Construction (Linux)
- Java memory model subclasses (Programming)
- Spring loaded container finishes executing a method (Programming)
- Let Linux boot methods to enter characters interface and set FrameBuffer resolution methods (Linux)
- CentOS 7.0 Automatic installation CD-ROM production Comments (Linux)
- Linux mention the right notes (Linux)
- MySQL root password reset under CentOS (Database)
- Install Ubuntu Software Center App Grid (Linux)
- Apache Tomcat integration and resin (Server)
- Configuring Android NDK development environment for Mac OS X (Linux)
- Report generation CPU, memory, and input and output ports with the sar command (Linux)
- Ubuntu 15.10 install the latest Arduino IDE 1.6.7 (Linux)
- Linux hard drive failure Case Studies (Linux)
- Ubuntu Server security risk checks (Linux)
- Linux Open coredump (Linux)
- Zabbix monitoring different versions of RAID installation and monitoring and MySQL master-slave monitor (Server)
- Ubuntu installed Komodo editor by PPA (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.