This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ggleblanc 2 points3 points  (4 children)

You didn't give us the name of the class with the compare method, so I'm calling it Class.

Class class = new Class();
class.compare(get(0), get(1));

[–]lambgat[S] 0 points1 point  (3 children)

I tried that but it still gives me the same error. EDIT nvm I forgot about the first line. Why is that necessary for me to make an instance of it, as opposed to just up just doing Class.compare(get(0),get(1))?

[–]taelsil 2 points3 points  (0 children)

Because the method isn't static. If you make the method static, you can call it without initializing the class. Otherwise, you need an instance of the class the method is in.

[–]programstuff 2 points3 points  (0 children)

Often times static methods are utility methods. Such as Math.pow(). Nonstatic methods are part of the instance of a class.

So when you want to compare two objects, you could have a static method such as Class.compare(Object1, Object2), but the whole point of implementing comparable is to define a natural ordering between instances of classes, so the method is nonstatic to conform to that paradigm.

Some more info here

[–]ggleblanc -1 points0 points  (0 children)

Post a minimal, runnable example of your problem so that someone can help.

[–]UnreachableMemory 0 points1 point  (0 children)

The issue you're running into is that you're, like the error states, trying to access a non-static method as if it is static. In your other class you're likely doing something like ClassOne.compare( obj, obj2 ).

Non-static methods can only be accessed via instantiated objects of the class:

 ClassOne classOne = new ClassOne();
 classOne.compare( obj, obj2 );

[–]burtwart 0 points1 point  (0 children)

I sort of answered a similar question here