Java “可以打包本地”是什么意思?(IDEA检查)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30678457/
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-11 09:57:42  来源:igfitidea点击:

What does "Can be package local" mean? (IDEA Inspection)

javaintellij-ideainspection

提问by Jerry Z.

I used IntelliJ for "Inspect Code", and one of its results is:

我将 IntelliJ 用于“检查代码”,其结果之一是:

??Problem synopsis ?????Can be package local (at line 18(public class HeartBeat))

??问题概要???可以是本地包(在第 18 行(public class HeartBeat)

What does it mean, how can I fix it?

这是什么意思,我该如何解决?

it whole class is like this:

全班是这样的:

package com.xxxxxxxxxxx.app.xxxx;

public class HeartBeat
{
    private static final Logger LOG = LoggerFactory.getLogger( HeartBeat.class );
    private final File heartBeatFile;


    public HeartBeat( File heartBeatFile )
    {
        this.heartBeatFile = heartBeatFile;
    }


    public void beat()
    {
        try
        {
            FileUtils.writeStringToFile( heartBeatFile, String.valueOf( System.currentTimeMillis() ) );
        }
        catch( IOException e )
        {
            LOG.error( "Error while writing heart beat log", e );
        }
    }
}

采纳答案by Andrej Herich

IDEA is referring to package-privatevisibility.

IDEA 指的是包私有可见性。

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package

可以使用修饰符声明一个类public,在这种情况下,该类对任何地方的所有类都是可见的。如果一个类没有修饰符(默认,也称为包私有),它只在它自己的包内可见

For more information, see Controlling Access to Members of a Class.

有关更多信息,请参阅控制对类成员的访问

You can solve the problem by removing publickeyword from the class (if the class is not intended to be used outside the package), or by using the class from a different package.

您可以通过public从类中删除关键字(如果该类不打算在包外使用)或使用不同包中的类来解决问题。

回答by devonlazarus

Your HeartBeat class isn't used anywhere outside of package com.xxxxxxxxxxx.app.xxxx. In this case you can declare the class 'protected' or 'private' to be more precise in your access.

您的 HeartBeat 类不在包 com.xxxxxxxxxxx.app.xxxx 之外的任何地方使用。在这种情况下,您可以在访问中更精确地声明“受保护”或“私有”类。

If you do not intend to use this class outside of this package, you should change the class declaration. If you intend to use this class outside this package, leave it and the warning will go away.

如果您不打算在此包之外使用此类,则应更改类声明。如果你打算在这个包之外使用这个类,离开它,警告就会消失。

E.g.:

例如:

protected class HeartBeat {
    ...
}

回答by ashario

If you want to suppress this warning:

如果要取消此警告:

  1. Go to Preferences -> Editor -> Inspections
  2. Go to Java -> Declaration redundancy
  3. Select "Declaration access can be weaker"
  4. Uncheck the "Suggest package local visibility..." checkboxes on the right
  1. 转到首选项 -> 编辑器 -> 检查
  2. 转到 Java -> 声明冗余
  3. 选择“声明访问可以更弱”
  4. 取消选中右侧的“建议包本地可见性...”复选框

EDIT: in the latest IDEA release step 4 this looks to have changed to "Suggest package-private visibility level for ..." and includes several options for various conditions

编辑:在最新的 IDEA 版本第 4 步中,这看起来已更改为“为...建议包私有可见性级别”,并包含适用于各种条件的多个选项

回答by Wahib Ul Haq

Coming from the Android app background, you also need to consider that sometimes those warnings are redundant. So for debug buildclasses, it can be package localbut for release buildit might be used outside of package.

来自 Android 应用程序后台,您还需要考虑有时这些警告是多余的。所以对于debug build类,它可以package local但是因为release build它可能在包之外使用。

I have disabled it on my end and @ashario answer was quite helpful in finding how to do it.

我已经禁用了它,@ashario 的回答对找到如何去做很有帮助。

回答by seekingStillness

Each of these lint warnings can be suppressed on a case-by-case basis with the following code.

这些 lint 警告中的每一个都可以使用以下代码逐案抑制。

@SuppressWarnings("WeakerAccess")