Question about #41 Building Objects for Constructors

0 Kathy Kayler · August 3, 2015
Hello,

I was just wondering if someone could further explain the code from tuna.java. I understand that the tuna objects pass in parameters to the appropriate constructors, but I don't understand how the this keyword passes these parameters into the following code:

public tuna(int h, int m, int s){
setTime(h,m,s);
}

In other words, I don't understand how the setTime method is called by tunaOject, tunaObject1, tunaObject2, or tunaObject3. 

Thank you in advance!

Post a Reply

Replies

Oldest  Newest  Rating
0 Zach Auringer · August 5, 2015
Because you called tuna() from your tunaObject, the computer is using information from the tunaObject's class.When you call setTime() from within the method, the computer knows you are still in the tunaObject's class and doesn't need to know where to get the method from. The object before the method is used to point where to find the method's location. If the class you are working in contains the method, a specific object is not needed to call the method. I hope this helps!
0 Kathy Kayler · August 7, 2015
Thanks a lot for your reply. I'm still a little confused however - I'll try to explain my question better with an example.

So I understand that in the main class, a tuna object is created with two parameters:

tuna tunaObject3 = new tuna(5,13);

This is passed into the appropriate constructor in the tuna class that takes two parameters:

public tuna(int h, int m){
this(h,m,0);
}

What I'm confused about is now is what does the this keyword do, since it takes the parameters and sets them as variable h and m (rather than hour and minute), and how is the setTime() method ever called in this case? 


Here's the code I copied from the tutorial, by the way.

apples.java
class apples {
public static void main(String [] args) {

tuna tunaObject = 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", tunaObject.toMilitary());
System.out.printf("%s\n", tunaObject2.toMilitary());
System.out.printf("%s\n", tunaObject3.toMilitary());
System.out.printf("%s\n", tunaObject4.toMilitary());
}
}



tuna.java
public class tuna {
private int hour;
private int minute;
private int 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){
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 toMilitary(){
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
}
0 Jasmin Bakalović · August 7, 2015
Java documentation:

"Within an instance method or a constructor, this is a reference to the current object - the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using  this."

Also, you can use this with dot notation "this." and in that case you will probably call some instance variable to set:


class People {

    private String name;

    public People(String name) {
         
         // Here you are referring to instance variable of a class called name using this
         // to set name throughout constructor who has one argument
         // If you try to write here name = name, you'll get an error, not an error but yellow
         // mark that says something like "The variable name has no effect"
         this.name = name;    

    }

}

Back to your code, you are confused what this is used here for, let's try to explain.

You have three constructors formed on this way:

First constructor accepts no arguments. But inside that constructor you are calling another constructor that accepts three parameters (int, int, int). Compiler then finds a constructor with three int arguments and those values you passed to the constructor he copies to the variables h, m, s.

Inside that constructor you are calling a method setTime and that method accepts three parameters, again int value, and you pass variables of the constructor.

Now, setTime is taking those values and sets them to the hour, minute and second.

Second constructor accepts one parameter and that parameter is int value. Inside that constructor you are calling again a constructor that accepts three parameters but in this case you passed your variable of constructor as a first parameter and two others are 0 (this(h, 0, 0)).

Then compiler repeat this step:

Inside that constructor you are calling a method setTime and that method accepts three parameters, again int value, and you pass variables of the constructor.

Now, setTime is taking those values and sets them to the hour, minute and second.

You probably know now what third constructor will do :)

Output:

00:00:00
05:00:00
05:13:00
05:13:43

If this was confused try to run this code and see how it behave.


class Person {
    
    private String name;
    private int id;
    
    public Person() {
        
        this("Kathy", 0);
        
        System.out.println("First constructor is running.");
        
    }
    
    public Person(String name) {
        
        this(name, 1);
        
        System.out.println("Second constructor is running.");
        
    }
    
    public Person(String name, int id) {
        
        setInfo(name, id);
        
        System.out.println("Third constructor is running.");
        
    }
    
    public void setInfo(String name, int id) {
        
        this.name = name;
        this.id = id;
        
    }
    
    public String getName() {
        
        return name;
        
    }
    
    public int getId() {
        
        return id;
        
    }
    
}

public class Application {
    
    public static void main(String[] args) {
        
        Person p1 = new Person();
        
        System.out.println();
        
        Person p2 = new Person("Zach");
        
        System.out.println();
        
        Person p3 = new Person("Jasmin", 2);
        
    }
}
0 Kathy Kayler · August 7, 2015
Thank you so much for the detailed explanation and example Jasmin! I understand now :)

And thank you for helping as well, Zach. Your replies are both much appreciated.
  • 1

Java / Android Development

107,051 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator