-2

I'm getting the error "error: recursive constructor invocation", this seems to correlate with not having a constructor that is initialized with parameters, and just using the this(), I have all the constructor signatures different, so I'm not sure what the problem is. Am I somehow not using this() correctly?

public class passenger{
    private int checkedBags;
    private int freeBags;
    private double bagsFee;
    public int getCheckedBags(){
        return this.checkedBags;
    }
    public void setCheckedBags(int checkedBags){
        this.checkedBags=checkedBags;
    }
    public int getFreeBags(){
        return this.freeBags;
    }
    public void setFreeBags(int freeBags){
        this.freeBags= freeBags;
    }
    public double getBagsFee(){
        return this.bagsFee;
    }
    public void setBagsFee(double bagsFee){
        this.bagsFee=bagsFee;
    }


   //all the examples I looked up online seem to correlate with one constructor not being intitialized
   passenger(int checkedBags, int freeBags, double bagsFee){//this is
        this.checkedBags= checkedBags;
        this.freeBags= freeBags;
        this.bagsFee= bagsFee;
    }
      passenger(int freeBags){
        this(freeBags);//giving error
    }
    passenger(double bagsFee){
        this(bagsFee);//giving error
    }
    passenger(){

    }
    public static void main(String[] args){
        passenger john= new passenger();
        passenger kate= new passenger(2,1,100d);
        System.out.println(john.getCheckedBags());
        System.out.println(john.getFreeBags());
        System.out.println(john.getBagsFee());

        System.out.println(kate.getCheckedBags());
        System.out.println(kate.getFreeBags());
        System.out.println(kate.getBagsFee());

    }

}

Samatha
  • 45
  • 5
  • 4
    Possible duplicate of [What does "this()" method mean?](http://stackoverflow.com/questions/15867722/what-does-this-method-mean) – csirmazbendeguz Mar 28 '17 at 16:16
  • What do you think `this(freeBags);` and `this(bagsFee);` do? – tnw Mar 28 '17 at 16:17
  • 2
    `this(freeBags);` calls `passenger(int freeBags)` which calls `this(freeBags);` which calls `passenger(int freeBags)`... – Iłya Bursov Mar 28 '17 at 16:17
  • @Samatha Given that you are stuck on the same problem for a couple of hours and asking multiple questions on StackOverflow about it, I think you should do some reading first... This is a good place to start: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html – Jaroslaw Pawlak Mar 28 '17 at 16:34
  • @Samatha and here is the one specific to your problem: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – Jaroslaw Pawlak Mar 28 '17 at 16:36
  • Thanks! I have a book but that seems a bit better – Samatha Mar 28 '17 at 16:38
  • 1
    The class should be named `Passenger`, not `passenger`. – Lew Bloch Mar 28 '17 at 17:24

1 Answers1

3

Your error constructors are calling themselves:

passenger(int freeBags){
    this(freeBags); // what do you expect this to do?
}

"Chaining" involves calling a different constructor, not just re-invoking the same one (which would infinitely re-invoke itself). So, for example, you'd call your first constructor with perhaps default arguments:

passenger(int freeBags){
    this(0, freeBags, 0.0d);
}

So ultimately you'd have a "primary" constructor which performs the actual construction logic, and the overloaded constructors are pass-throughs to that one.

David
  • 208,112
  • 36
  • 198
  • 279