0

For one of my programming assignments, I was tasked to write a couple methods. I noticed that one of my methods could be expressed as using an if-else statement between two methods I already defined. I cannot change the method signatures to make those two other methods static.

I heard somewhere/was briefly taught that you can add this in front of the method (this.method()). Does that change it so that instance methods could be use statically? What does adding this specifically do? Is it "good practice" to use it like this?

Pseudocode for two methods:

public boolean methodA(input){
    //does something
    return true or false depending on something;
}
public boolean methodB(input){
    //does something else
    return true or false depending on something else;
}

I noticed after coding that I can call the methods inside each other without using this so my question really is, which is better/more reliable? Using this vs. not using this???

public boolean methodAB(input){
    boolean present = input is already present;
    if(present) {return methodA();}
    else {return methodB();}
}

or...

public boolean methodAB(input){
    boolean present = input is already present;
    if(present) {return this.methodA();}
    else {return this.methodB();}
}
LB2727
  • 1
  • 1
    " Does that change it so that instance methods could be use statically?" No. "What does adding this specifically do?" Nothing; it's fully equivalent to use it or to leave it out. "Is it "good practice" to use it like this?" No. – Louis Wasserman Jun 02 '23 at 21:18

0 Answers0