开发者

How to resolve "static method ___ should be accessed in a static way" warnings

I'm going through the book Just Java 2 but am evidently missing something basic. These are two separate projects. I've generated a JAR for the second and added it to the first's build path. The correct areas are printed but the compiler generates these warnings. How would these be resolved?

// -----------------------------------------------------------
// Testing.java
// -----------------------------------------------------------
public class Testing {
    public static void main(String[] args) {
        RectangleDFC r = new RectangleDFC(3, 4);
        System.out.println(r.Area());
            // WARNING: The static method Area() from the type RectangleDFC
            //          should be accessed in a static way
        r.SetSides (10, 10);
            // WARNING: The static method SetSides(int, int) from the type
            //          RectangleDFC should be accessed in a static way
        System.out.println(r.Area());
            // WARNING: The static method Area() from the type RectangleDFC
            //          should be accessed in a static way
    }
}
// -----------------------------------------------------------
// RectangleDFC.java
// --开发者_如何学Go---------------------------------------------------------
public class RectangleDFC {
    int side1;
    int side2;
    RectangleDFC(int s1, int s2) {
        SetSides(s1, s2);
    }
    public void SetSides(int s1, int s2) {
        side1 = s1;
        side2 = s2;
    }
    public int Area() {
        return side1 * side2;
    }
}


First off; methods in Java should be lowerCamelCase(), not UpperCamelCase(). Class names should be UpperCamelCase().

Second;

int side1;
int side2;

should be

private int side1;
private int side2;

and preferably ( if you aren't modifying them )

private final int side1;
private final int side2;

and you should just set the side1 and side2 inside the constructor instead of the setter.

That said, I don't think you are executing the code you posted, there is no reason those errors should be emitted with the code you posted, you are probably linking to a .jar file with some old code where the area() method is declared static.

Also that book is pretty old in Internet time, there are much better beginner books that cover "modern" Java much better. For example, if the book you are using is using Enumeration, Vector or Hashtable put it in the trash and get a newer book.


The shown code will not generate the warning. Maybe you changed the code and forgot to update the JAR in the classpath of Testing class?

And yes, you definitely should adhere to naming conventions as shown by fuzzy lollipop.


Is there some additional code that you aren't showing here that has Area and SetSides defined as static methods? If so, and that code is higher on the classpath than the version you are showing here, that is the problem. As Peter Kofler mentions the code you are displaying will not generate that warning. To get rid of the warnings you would have to replace r.Area() with Rectangle.Area() and r.setSides(10, 10) with Rectangle.SetSides(10, 10).

That being said it makes no sense for those methods to be static. Also, see fuzzy lollipop's comment for proper Java conventions.


Use the class name instead of the instance name when indicated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜