SRL Archive - General

The static method should be accessed in a static way


rj

2014-11-21 21:35:00

I have a question, been working on a Java game and I am wondering what eclipse means when it gives me the warning:


Box.class:

package main;

public class Box {

public final int x1;
public final int y1;
public final int x2;
public final int y2;

public Box(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}

public static boolean collision(Box box1, Box box2) {
return !((box1.y2 < box2.y1) || (box1.y1 > box2.y2) || (box1.x1 > box2.x2) || (box1.x2 < box2.x1) );
}

public boolean collision(Box box) {
return this.collision(this, box);
}

}

this line:

		return this.collision(this, box);

riwu

2014-11-21 21:36:39

u can only invoke a static method this way: Box.collision()

rj

2014-11-21 21:39:56

From: riwu
u can only invoke a static method this way: Box.collision()


Is there a advantage of keeping them static or should I just change to public boolean instead of public static boolean

riwu

2014-11-21 21:43:15

From: Robert
Is there a advantage of keeping them static or should I just change to public boolean instead of public static boolean

As long as the method is not dependent on instance creation, and that you are sure that there would never be a need to override it.

rj

2014-11-21 21:45:27

From: riwu
As long as the method is not dependent on instance creation, and that you are sure that there would never be a need to override it.


Thanks, might be replying back here in a few trying to clean up a game I made a while back