Java 如何解决“静态方法 ___ 应该以静态方式访问”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3198135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:18:58  来源:igfitidea点击:

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

javaclassstaticwarnings

提问by DavidC

I'm going through the book Just Java 2but 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?

我正在阅读Just Java 2这本书,但显然缺少一些基本的东西。这是两个独立的项目。我为第二个生成了一个 JAR,并将其添加到第一个的构建路径中。打印了正确的区域,但编译器会生成这些警告。这些将如何解决?

// -----------------------------------------------------------
// 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
// -----------------------------------------------------------
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;
    }
}

采纳答案by Thorbj?rn Ravn Andersen

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

首先; Java 中的方法应该是lowerCamelCase(),而不是UpperCamelCase()。类名应该是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 side1and side2inside the constructor instead of the setter.

并且您应该只在构造函数中设置side1andside2而不是设置器。

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.

也就是说,我不认为您正在执行您发布的代码,这些错误没有理由应该与您发布的代码一起发出,您可能链接到带有一些旧代码的 .jar 文件,其中area()声明了该方法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, Vectoror Hashtableput it in the trash and get a newer book.

这本书在互联网时代已经很老了,有更好的初学者书籍可以更好地涵盖“现代”Java。例如,如果您正在使用的书正在使用EnumerationVector或者Hashtable将其放入垃圾箱并获取更新的书。

回答by Thorbj?rn Ravn Andersen

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

指示时使用类名而不是实例名。

回答by Peter Kofler

The shown code will notgenerate the warning. Maybe you changed the code and forgot to update the JAR in the classpath of Testingclass?

显示的代码不会生成警告。也许您更改了代码而忘记更新类的Testing类路径中的 JAR ?

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

是的,您绝对应该遵守模糊棒棒糖所示的命名约定。

回答by laz

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).

是否有一些您没有在此处显示的附加代码将 Area 和 SetSides 定义为静态方法?如果是这样,并且类路径上的代码比您在此处显示的版本高,那就是问题所在。正如 Peter Kofler 提到的,您正在显示的代码不会生成该警告。为了摆脱的警告,你会不得不更换r.Area()Rectangle.Area()r.setSides(10, 10)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.

话虽如此,这些方法是静态的是没有意义的。另外,请参阅模糊棒棒糖的评论以了解正确的 Java 约定。