this inside a builder, what does it do?

Asked

Viewed 84 times

4

good , can anyone tell me what this does ?

public Tempo (){
    this(0,0,0);
} 
public Tempo (int h){

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

    this (h,m,0);
}
public Tempo (int h,int m , int s){
    SetTime(h,m,s);

}

1 answer

5


The construction this inside the constructor serves to call another constructor.

That is to say this(0,0,0); will call the builder

public Tempo (int h,int m , int s){
    SetTime(h,m,s);
}

It cannot be used in a conventional method and must be the first instruction within the constructor, that is, if you want to use it. It’s just a way to maintain the object initialization logic in only one of the constructors.

  • Calling the constructor means that it will execute the contents of that constructor ,ie the setTime method ?

  • It’s perfect, Bruno, perfect. Just to complement, what will define which constructor will be called, are the types and the amount of parameters passed in this method().

  • @Pedrogouveia Yes Peter, he will call the setTime

Browser other questions tagged

You are not signed in. Login or sign up in order to post.