Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java polymorphic methods inside constructors complete analysis     - C # how to generate a folder or file automatically rename (Programming)

- RHEL 6.5 x86_64 CentOS yum configuration source (Linux)

- NAT and firewall under Linux (Linux)

- To install HDRMerge 0.4.3 under ubuntu (Linux)

- Install Web-based monitoring tool: Linux-Dash (Server)

- Using PPA to install the lightweight theme software HotShots 2.1.0 under Ubuntu (Linux)

- How to import JNI resulting .so libraries in Android Studio (Programming)

- Kali Linux 2.0 U disk installation errors Your installation cd-rom could not be mounted (Linux)

- Vim highlight lookup operation (Linux)

- C language header file defines a global variable (Programming)

- Linux disk quota-related instruction (Linux)

- Automated Password Generator: Linux under a special password generator (Linux)

- Using Linux command line and execute PHP code (Programming)

- libnet list of functions (Programming)

- Linux environment RabbitMQ installation and monitoring of plug-in installation (Linux)

- Python programmers most often committed ten errors (Programming)

- Why do you need close contact Rust 1.0 (Programming)

- Linux (RHEL5) general optimization system security (Linux)

- Applications Linux leap second problem caused (Linux)

- Use Vagrant up a local development environment tutorials (Server)

  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.
- Ubuntu 14.04 build Gitlab (Linux)
- Detailed Linux network security policies and protection measures (Linux)
- Nginx request processing (Server)
- Linux LVM space expansion Demo (Linux)
- Linux file content inspection - cat, tac, no, more, less, head, tail, od (Linux)
- Oracle 12c PDB Analysis (Database)
- Oracle view object space usage show_space (Database)
- CentOS 7.0 Enable iptables firewall (Linux)
- Hibernate + JUnit test entity class generate database table (Programming)
- Ubuntu 14.04 Install WordPress on Nginx (Server)
- Thrift 0.9.3 compiler installation under Ubuntu (Linux)
- Java object initialization (Programming)
- OpenStack Folsom create an instance of the failure of the Quota Fixed_IP articles (Server)
- Monitor log file (listener.log) (Database)
- Linux user login ban (Linux)
- System Safety: Windows and Linux platforms (Linux)
- Quickly locate the mistakes by gdb location (Programming)
- MySQL main and backup replication structures (using mysqld_multi) (Database)
- Oracle conditional select statements and looping statements (Database)
- MariaDB database storage path modify configuration issues (Database)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.