Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By kaydell2

Thread: Multiple Constructors Help

  1. #1
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Multiple Constructors Help

    So I'm practicing Java, and I got this code from a Youtube video. The video teaches how to use multiple constructors with different arguments. When I run the program, I get this error message:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Recursive constructor invocation Tuna(int, int, int)
    Recursive constructor invocation Tuna(int, int, int)
    Recursive constructor invocation Tuna(int, int, int)
    Recursive constructor invocation Tuna(int, int, int)

    at time.Tuna.<init>(Tuna.java:8)
    at time.Apples.main(Apples.java:7)

    My code is almost identical to the video's code, I don't know why it's not working. Any help? Thanks.
    Edit: I'm getting errors on the lines that use "this" in the Tuna class. This is kind of an old video, has Java changed since then?

    Apple class:
    Java Code:
    package time;
    
    public class Apples {
    
    	public static void main(String[] args) {
    
    		Tuna tunaObject1 = new Tuna();
    		Tuna tunaObject2 = new Tuna(5);
    		Tuna tunaObject3 = new Tuna(5,13);
    		Tuna tunaObject4 = new Tuna(5,13,43);
    		
    		System.out.printf("%s\n", tunaObject1.toMil());
    		System.out.printf("%s\n", tunaObject2.toMil());
    		System.out.printf("%s\n", tunaObject3.toMil());
    		System.out.printf("%s\n", tunaObject4.toMil());
    
    	}
    
    }
    Tuna class:
    Java Code:
    package time;
    
    public class Tuna {
    	
    	private int hour, minute, second;
    	
    	public Tuna(){
    		this(0,0,0);
    	}
    	
    	public Tuna(int h){
    		this(h,0,0);
    	}
    	
    	public Tuna(int h,int m){
    		this(h,m,0);
    	}
    	
    	public Tuna(int h,int m,int s){
    		this(h,m,s);
    		setTime(h,m,s);
    	}
    	
    	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){
    		minute = ((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 minute;
    	}
    	
    	public int getSecond(){
    		return second;
    	}
    	
    	public String toMil(){
    		return String.format("%02d:%02d:%02d",getHour(),getMinute(),getSecond());
    	}
    
    }
    Last edited by DarrylBurke; 12-07-2012 at 06:19 AM. Reason: Removed link to video

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Multiple Constructors Help

    The constructor with three parameters was calling itself. I commented it out.


    public Tuna(int h,int m,int s){
    // this(h,m,s); <<<<< this constructor can't call itself.
    setTime(h,m,s);
    }
    Fubarable likes this.

  3. #3
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Using multiple constructors from a class.
    By BradMD in forum New To Java
    Replies: 6
    Last Post: 08-09-2012, 03:53 AM
  2. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 12:55 PM
  3. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 12:05 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 07:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •