2

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

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

    if(this.denominator == 0 )
    {
     throw new FractionException(" Undefined Fraction ");
    } 
}

I also have a method :

 public FractionInterface multiply(FractionInterface secondFraction)
 {
    Fraction second = (Fraction) secondFraction; 
    Fraction answer = new Fraction ((numerator * second.numerator), (denominator * second.denominator));        
    answer.reduceToLowestTerms();
    return answer; 
}

The above method works fine when I compile and run but so this this version:

 public FractionInterface multiply(FractionInterface secondFraction)
 {
    Fraction second = (Fraction) secondFraction; 
    Fraction answer = new Fraction ((this.numerator * second.numerator), (this.denominator * second.denominator));      
    answer.reduceToLowestTerms();
    return answer; 
 }

My question is which one is correct ?? If use the "this" keyword in my constructor do I also have to use it in my methods ?? Again, they both work fine and do what they are supposed to do but I want to know which way is the correct way. Thanks.

lrod408
  • 135
  • 7
  • the use of `this` in the constructor (or setters) is usually used to disambiguate the variable names – Scary Wombat Mar 02 '15 at 08:24
  • 1
    In terms of `this`, both are correct. The bigger issue with both `multiply` implementations is that they **lie**: They claim to accept anything implementing the `FractionInterface`, but in fact they only accept instances of `Fraction` and `Fraction` subclasses. A correct implementation would work with `FractionInterface` (or declare the argument of type `Fraction` instead). – T.J. Crowder Mar 02 '15 at 08:27
  • But on `this`: There are two schools of thought on using `this` on the occasions it's optional: The school who feel that since it's not necessary, it's just extra typing/text, and the school who feel that it's useful for clarity. If you don't use `this`, it's easy to think that `numerator` is a function argument or local variable rather than an instance field. – T.J. Crowder Mar 02 '15 at 08:29
  • Consider this example for more http://stackoverflow.com/questions/2429062/java-when-to-use-this-keyword – shikjohari Mar 02 '15 at 08:37
  • You should aim to eliminate redundant notation wherever encountered. – user207421 Mar 03 '15 at 09:05

3 Answers3

4

Using this explicitly is mandatory if you wish to distinguish between a local variable and a member of the same name. Otherwise, it is optional.

Your constructor won't assign the passes values to the instance members without the this. prefix, since the method arguments would hide the instance members. If you give the arguments different names, you can do without the this. :

public Fraction(int num, int denom)
{
    numerator = num; 
    denominator = denom;
    ...
}

Both multiply versions are the same.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    E.g., both `multiply` method implementations are "correct" (other than that cast!). – T.J. Crowder Mar 02 '15 at 08:25
  • why is the cast incorrect ?? according to my professor I had to downcast reference parameter FractionInterface to Fraction if I want to use it as Fraction. – lrod408 Mar 02 '15 at 08:32
  • @lrod408 The cast has the potential of throwing ClassCastException, since you don't verify that `secondFraction` is an `instanceof Fraction` before casting it. If multiple classes implement `FractionInterface`, the cast would fail for some of them. – Eran Mar 02 '15 at 08:36
  • @T.J Crowder, thanks for the heads up. This time only the `Fraction` class implements `FractionInterFace` but that is still good to know. Thanks again :) – lrod408 Mar 02 '15 at 08:43
2

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

shikjohari
  • 2,278
  • 11
  • 23
1

I think you have a bit of a confusion on how the "this" keyword works.

Let me give you an example:

This

public class testing {
    private int a;
    private int b;

    testing(int a,int b){
        this.a = a;
        this.b = b;
    }
}

is the same as:

public class testing {
    private int a;
    private int b;

    testing(int x,int y){
        this.a = x;
        this.b = y;
    }
}

Which of course for the second one would be easier to put if we do it like this:

public class testing {
    private int a;
    private int b;

    testing(int x,int y){
        a = x;
        b = y;
    }
}
Kiong
  • 798
  • 1
  • 8
  • 28