1

I have a class ObjectX, and two objects of that same class objectX1, and objectX2.

I also have a method that checks which of two objects of that class is the biggest. Each object of the class consists in a large number stored in an arrayList.

Here's the method declaration:

public ObjectX bigger(ObjectX objectX2);

This method is working just fine, it takes another object besides the one from the class itself, and returns which one is the biggest.

My problem though is when i try to call that method inside an if statement, like this:

public static void main(String[] args) {

if((this.bigger(object2)) == this)
    System.out.println("First is bigger!");
else if((this.bigger(object2)) == object2)
    System.out.println("Second is bigger!");
else
    System.out.println("The objects are equal!");

}

The if statement does not check as it was supposed to. How can i solve this ?

laker001
  • 417
  • 7
  • 20

3 Answers3

3

There is a number of issues with your attempt.

[edit - thanks Tim Biegeleisen for spotting this]

Firstly, you reference this in a static context, whereas a class-scoped context cannot know of a specific instance on its own.

Secondly, == is used to compare object references. You should use equals instead.

But in this case, it might not work, depending on your implementation of method bigger.

What I strongly recommend is to implement Comparable<MyObject> instead.

Replace MyObject with either the type of the objects in question, or some interface for all objects you with to compare.

The compareTo(T o) method can the be implemented with the logic in your bigger method, and more!

By contract, it should return:

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object

See API here.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    The biggest problem as I see it is that he is using `this` inside a `static` method. This is a definite no-go. – Tim Biegeleisen Mar 23 '15 at 11:34
  • With the way that `bigger` has been implemented, using `==` is correct, given that s/he wants to check that the object returned is the same object. Don't just see `==` and assume it is wrong. – James Mar 23 '15 at 11:34
  • @TimBiegeleisen I hadn't noticed. Yes, that is a red flag :) – Mena Mar 23 '15 at 11:35
  • @Mena so basically the body of my compareTo function is equal to my bigger function, is that right ? – laker001 Mar 23 '15 at 12:07
  • @laker001 not exactly. `bigger` supposedly returns the bigger object. `compareTo` returns an `int`, whose sign and value depend on the comparison between `this` object and the given argument. However the logic for comparing which is bigger should be reusable. – Mena Mar 23 '15 at 12:09
  • @Mena that's what i thought, but i'm struggling with what i need to write inside the comparaTo method. if i write it equal to bigger it works just fine, however when i make an if statement like the one in my question it gives me an error – laker001 Mar 23 '15 at 12:25
  • @laker001 look at it this way: think of what makes one of your objects bigger than the other. Maybe the value of some property, or some properties combined? Then use this logic by comparing the properties of `this` with the properties of the object passed as argument, and based on the outcome, return `-1`, `0` or `1` for a simple result, or higher/lower values and `0` if you can actually **quantify** by how many units a given object is "bigger" / "smaller than the other. Then you invoke the `compareTo` method and base your `if` condition on the `int` it returns. – Mena Mar 23 '15 at 12:38
  • yes, but those properties are being compared in the bigger method! so i ultimately have to rewrite some code @Mena – laker001 Mar 23 '15 at 12:51
  • @laker001 you just have to adapt it so it fits the context and returns an `int` instead of an object. – Mena Mar 23 '15 at 13:18
3

You cannot use this in a static method like the main method. You can only use it in an instance method.

alainlompo
  • 4,414
  • 4
  • 32
  • 41
3

Make your object class implement Comparable and override the compareTo method, this way you can define the conditions of assessing which object of any type is bigger.

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55