java “this” keyword proper use

前端 未结 3 1289
[愿得一人]
[愿得一人] 2021-01-15 02:01

I have a Fraction class using keyword this in my constructor:

public Fraction(int numerator, int denominator)
{
    this.numerator = numerator; 
    this.         


        
3条回答
  •  情深已故
    2021-01-15 02:59

    Both the cases you mentioned will work fine.

    Use of this is a good practice as its more readable due to our english mindset, When we say this.some_variable, we get a mental image of some_variable inside the current class

    this keyword is also helpful in avoiding variable shadowing

提交回复
热议问题