-1

I'm currently having a self-study with java, watching thenewboston's vids. I coded a constructor just like his code but I'm getting an error.

The code from the tutorial compared to my code

Here's my complete code in another.java

NOTE: THE SCREENSHOT ABOVE AND THE CODES BELOW DOES NOT HAVE THE SAME NAME OF CLASS, I JUST CHANGED IT BECAUSE IT CONFUSED ME. HOPE YOU UNDERSTAND

        public class class2{
          private int hour;
          private int minutes;
          private int second;

        public class2(){
            this(0,0,0); /** It says "recursive constructor incovation class2(int,int,int)" */

        }

        public class2(int h){
            this(h,0,0); /** It says "recursive constructor incovation class2(int,int,int)" */
        }

        public class2(int h, int m){
            this(h,m,0); /** It says "recursive constructor incovation class2(int,int,int)" */
        }

        public class2(int h, int m, int s){
            this(h,m,s); /** It says "recursive constructor incovation class2(int,int,int)" */
        }
        public void setTime(int h, int m, int s){
            setHour(h);
            setMinute(m);
            setSecond(s);
        }
        public void setHour(int h){
            hour = ((h >=0 && h <24) ? h : 0);
        }
        public void setMinute(int m){
            minutes = ((m >=0 && m <60) ? m : 0);
        }
        public void setSecond(int s){
            second = ((s >=0 && s <60) ? s : 0);
        }
        public int getHour (){
            return hour;
        }
        public int getMinute (){
            return minutes;
        }
        public int getSecond(){
            return second;
        }
        public String printTime(){
            return String.format("%02d:%02d:%02d:", getHour(),getMinute(),getSecond());
        }
    }   

The error it says is "recursive constructor incovation class2(int,int,int)" If you have an answer, kindly explain it also. Thanks!

jerome_mjt
  • 280
  • 3
  • 12
  • 1
    That's what happens when you make a constructor that calls itself. At least the compiler is smart enough to warn you, rather than just letting your program crash at runtime from a stack overflow. – azurefrog Jun 10 '16 at 17:14

1 Answers1

4

Because this is recursive:

public class2(int h, int m, int s){
    this(h,m,s); /** It says "recursive constructor incovation class2(int,int,int)" */
}

Calling this constructor... calls this constructor. Infinitely.

If this is the "main" constructor (the one all the others call), then this is where you'd want to perform your actual constructor logic. Something like:

public class2(int h, int m, int s){
    setTime(h, m, s);
}
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    As a side-note, calling overridable methods (like `setTime`) from the constructor [is generally not a good idea](http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors), it would be preferable to just initialize the variables with `this.h = h`, etc. – Tunaki Jun 10 '16 at 17:22
  • @Tunaki: True. Since there's logic a couple methods deep in that structure, I imagine that logic should ideally be refactored into private setters which are invoked by the overridable ones. – David Jun 10 '16 at 17:23
  • @David,So this thenewboston thing is teaching wrong ways to code? – jerome_mjt Jun 11 '16 at 04:19
  • @jerome_mjt: If their code has the same error, then yes. If not, then it would appear that you'd copied it incorrectly? – David Jun 11 '16 at 09:12