3
    package rups;

    public class vipcustomer{


    private String name;
    private int creditlimit;
    private String emailid;

    public vipcustomer(){
    this("Rupali", 5000, "rupalisingh@gmial.com");
    System.out.println("Constructer with default values");
    }

    public vipcustomer(int creditlimit, String emailid) {
        this("Rups", creditlimit, emailid);
        this.creditlimit = creditlimit;
        this.emailid = emailid;
        System.out.println("Constructor with 1 default values");
    }

    public vipcustomer(String name, int creditlimit, String emailid) {
        this.name = name;
        this.creditlimit = creditlimit;
        this.emailid = emailid;
        System.out.println("Constructer with no default values");
    }
    public String getName() {
        return name;
    }

    public int getCreditlimit() {
        return creditlimit;
    }

    public String getEmailid() {
        return emailid;
    }
    }

    public class Main {
        public static void main(String args[]){
            new vipcustomer();
            new vipcustomer(5000, "sdhoahfsdh");
            new vipcustomer("Rups", 7000, "dfksjdfsjdfa");


    }
    }

output

Constructer with no default values
Constructor with 1 default values
Constructer with no default values

Here the 1st constructor should give output as "Constructor with default values" but it is not so. What am i doing wrong?please help.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • its says "Constructor with no default value". But when I am calling the default constructor it should print "Constructor with default values". –  Jan 06 '18 at 17:52
  • 3
    What do you think `this(...)` is doing? – Hovercraft Full Of Eels Jan 06 '18 at 17:53
  • 1
    Side note: why do you set fields doubly? Why the redundant code in this `public vipcustomer(int creditlimit, String emailid) {` constructor. The call to `this` already sets fields for you. – Hovercraft Full Of Eels Jan 06 '18 at 17:53
  • ohhh....ok. So no need to use this. again for second constructor. –  Jan 06 '18 at 17:55
  • 1
    @RupaliSingh Read this: [Constructor Overloading](https://stackoverflow.com/questions/1182153/constructor-overloading-in-java-best-practice) – progyammer Jan 06 '18 at 17:56

2 Answers2

5

From within a constructor, you can use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.


Let's analyse your code, there are three constructors :

public vipcustomer()//------------------------------------------------(C1)
public vipcustomer(int creditlimit, String emailid)//-----------------(C2)
public vipcustomer(String name, int creditlimit, String emailid)//----(C3)

so when you use :

new vipcustomer();

It call this constructor :

public vipcustomer() {
    this("Rupali", 5000, "rupalisingh@gmial.com");
    System.out.println("Constructer with default values");
}

But note this("Rupali", 5000, "rupalisingh@gmial.com"); It call the the C3 when C3 finish it print :

Constructor with no default values

for that the first output is :

Constructor with no default values

then

Constructor with default values  

The same for the other constructors.

The this() function calls overloaded constructors according to the arguments list.

Take a look at this :

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

Cause for your wrong output is following default constructor.

public vipcustomer(){
    this("Rupali", 5000, "rupalisingh@gmial.com"); // Will call 3 argument constructor
    System.out.println("Constructer with default values");
}

Within the default constructor (No argument constructor) you have called

this("Rupali", 5000, "rupalisingh@gmial.com");

That means it will call the constuctor that you have provided with 3 arguments.

public vipcustomer(String name, int creditlimit, String emailid) {...}

This keyword

The this keyword has 2 meanings. In the constructor, this(...) (like method call) act as a call for the constructors. The compiler decides which constructor to call based on the number arguments and types of the arguments of the provided constructors.

In other case this keyword use as a reference.

Saveendra Ekanayake
  • 3,153
  • 6
  • 34
  • 44