简体   繁体   English

为什么只有使用 static 方法的 class 会出现错误:“Add a private constructor to hide the implicit public one.”?

[英]Why does only the class with the static method have the error: “Add a private constructor to hide the implicit public one.”?

Using Java.使用 Java。 The below is a single program that contains 2 methods that are called in another Class program that has the main (which proves that non-static methods require objects to be created first).下面是一个包含 2 个方法的单个程序,这些方法在另一个具有main的 Class 程序中调用(这证明了非静态方法需要先创建对象)。 HasAStaticMethod has an orange SonarLint error. HasAStaticMethod有一个橙色的 SonarLint 错误。 Why doesn't the class NotStatic have an error, too?为什么 class NotStatic也没有错误?

public final class Test {
   private Test () {
      }
    }

class HasAStaticMethod {

    //private HasAStaticMethod(){}

    public static void myPrint(String s) {
        System.out.println(s);
    }
}

class NotStatic {

    public void myPrint(String s) {
        System.out.println(s);
    }
}```

Why does only the class with the static method have the error: “Add a private constructor to hide the implicit public one.”?为什么只有使用 static 方法的 class 会出现错误:“Add a private constructor to hide the implicit public one.”?

Creating an instance of HasStaticMethod would be a programmer mistake since it can serve (almost) no useful purpose... as well as a harmful one (see below).创建HasStaticMethod的实例将是程序员的错误,因为它可以(几乎)没有用处......以及有害的用途(见下文)。

Declaring a private constructor will cause that programmer mistake (ie mistakenly instantiating HasStaticMethod ) to be flagged as a compilation error.声明私有构造函数将导致程序员错误(即错误地实例化HasStaticMethod )被标记为编译错误。

This is a good thing .这是一件好事


Why doesn't the class NotStatic have an error, too?为什么 class NotStatic 也没有错误?

Because you need an instance of NoStatic in order for call NoStatic.myPrint .因为您需要一个NoStatic实例才能调用NoStatic.myPrint So you need a non-private constructor to make the instance.所以你需要一个非私有的构造函数来创建实例。 A default constructor will do... because that will be public.默认构造函数会做......因为那将是公共的。

NoStatic.myPrint();              // compilation error
new NoStatic().myPrint();        // OK

You don't need an instance in the HasStaticMethod case.HasStaticMethod案例中不需要实例。 The correct way to use it is:正确的使用方法是:

HasStaticMethod.myPrint();       // OK

You could write this:你可以这样写:

new HasStaticMethod().myPrint(); // compiles ... but bad

... but it doesn't do what the reader (most likely) thinks it does. ...但它并没有像读者(很可能)认为的那样。 The instantiation of the class is pointless, and calling a static method via an instance reference is downright misleading. class 的实例化是毫无意义的,通过实例引用调用 static 方法完全是误导。 That is the reasoning behind the IDE hint: to stop the programmer (who is using your HasStaticMethod class) from accidentally or deliberately writing that kind of nonsense.这就是 IDE 提示背后的原因:阻止程序员(正在使用您的HasStaticMethod类)意外或故意编写这种废话。


I think you may be thinking about this from the wrong perspective 1 .我想你可能从错误的角度思考这个问题1 The goal is to write Java code that 1) works and 2) can be read and maintained by someone else.目标是编写 Java 代码,1)可以工作,2)可以被其他人阅读和维护。 To that end, it is good thing when the IDE / Sonar warns us we are doing something that is liable to lead to problems.为此,当 IDE / Sonar 警告我们我们正在做一些可能导致问题的事情时,这是一件好事。 (And indeed, this is why we use tools like Sonar.) (事实上,这就是我们使用声纳等工具的原因。)

Now you are free to twiddle with the Sonar settings to turn off this warning if you don't like it.现在,如果您不喜欢它,您可以随意调整声纳设置来关闭此警告。 Or stop using Sonar altogether.或者完全停止使用声纳。 (But check with your coworkers and manager first, because they might have some opinions on that course of action.) (但请先咨询您的同事和经理,因为他们可能对这一行动方案有一些意见。)

But my advice is to just add the private constructor and declare the utility class as final ... as Sonar suggests.但我的建议是添加private构造函数并将实用程序 class 声明为final ... 正如 Sonar 所建议的那样。 It is (IMO) a good thing to do.这是(IMO)一件好事。


1 - This should not be about "freedom of expression" or "personal choice". 1 - 这不应该是关于“言论自由”或“个人选择”。 This is not poetry...这不是诗...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Calendar类没有公共构造函数? - Why does the Calendar class not have a public constructor? 在类中仅存根一个私有静态方法 - Stub only one private static method in a class 一个类可以同时具有公共和私有构造函数吗? - Can a class have both public and private constructor? 为什么私有基类构造函数导致“隐式超级构造函数不可见” - Why does a private base class constructor result in “Implicit super constructor is not visible” Spring java config to invoke a public non static method of a singleton class with private constructor - Spring java config to invoke a public non static method of a singleton class with private constructor 为什么要使用public和private来静态成员和方法? - Why use public and private to static members and method? 使用公共构造函数 Java 声明一个私有 static 嵌套 class? - Declare a private static nested class with a public constructor Java? 为什么在类中有公共静态类 - Why have public static class inside a class 私有的嵌套类(内部或静态)是否可以具有公共访问的方法? - Does nested class (inner or static) that is private may has method with public acesses? 为什么类的子类必须是静态的才能在类的构造函数中初始化子类? - Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM