简体   繁体   中英

Java Constructor problem, Output printed twice

I am new to java.

code:

class class1 {
        int x, y;
        class1() {
                this(12,8);
                this.mul();
        }
        class1(int x, int y){
                this.x = x;
                this.y = y;
                this.mul();
        }
        void mul() {
                System.out.println(x*y);
        }
}
public class this1 {
        public static void main(String args[]) {
                class1 obj1 = new class1();
                class1 obj2 = new class1(1, 5);

        }
}

When I run this code:

96
96
5

Question: Why is 96 printed twice. Is it that when I create an object with a parameterized constructor, first the control is passed through default constructor and then to the parameterized constructor

The reason why it prints two times 96 is this: When you create an object using the constructor with no parameters, you do two things:

this(12,8);
this.mul();

This means that if the constructor with no parameters is called, you need to call the constructor that accepts two parameters (int x, int y) and after that you need to call the function mul(); The function mul(); of the 1st invoked constructor will print the result.

Now since in the first constructor you also call the constructor with parameters you execute this:

this.x = x;
this.y = y;
this.mul();

As you can see after setting this.x = x and this.y = y, then you call once again this.mul(); which will print the result.

The fix you need to make sure you only print once per object is simple. Just remove the call to this.mul();in the constructor with no parameters Class1().

OLD Class1():

class1() {
            this(12,8);
            this.mul();
    }

NEW Class1():

class1() {
            this(12,8);
    }

In this way you'll call this.mul(); only once through the second constructor. Hope it helped!

Is it that when I create an object with a parameterized constructor, first the control is passed through default constructor and then to the parameterized constructor

No, it's the reverse.

Your parameterless constructor explicitly chains to the parameterized one:

class1() {
    this(12,8); // Call to the parameterized constructor (which will call mul once)
    this.mul(); // Second call to mul
}

When you create first object class1 obj1 = new class1(); this calls the default constructor, but see what is inside of your default constructor? Look at the code you wrote

  class1() {
                this(12,8);
                this.mul();
        }

here what happens is, this(12,8) calls the parameterized constructor of this class which is called chain of constructors.

class1(int x, int y){
            this.x = x;
            this.y = y;
            this.mul();
    }

and sets corresponding values to x and y and calls the mul function which comes out to be 96. And then next line evaluates and yes again it calls the same method [ mul ]. This is the reason you are getting 96 twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM