简体   繁体   中英

How to disable "Access can be package-private" message in IntelliJ?

Developing an API, the warning message "Access can be package-package" in Java classes can be really annoying.

I've already spent some time going through the settings to find a way to disable this message but without any findings. Any idea how to disable the message?

Go to Settings → Editor → Inspections then Java → Declaration redundancy → Declaration access can be weaker rule.
Here you can either disable it at all or select the suggestion options on the right pane:

在此输入图像描述

检查规则是“声明访问权限可能较弱”,有两个选项可以禁用“建议包 - 私有可见性级别......”

要根据具体情况禁用,请使用

@SuppressWarnings("WeakerAccess")

Move the cursor to public , press Alt + Enter and choose one of the options:
在此输入图像描述

Another solution not mentioned so far: if you have a method that's declared public, and the IDE is telling you that there are no references to the method from outside the package, then perhaps you need to add a test case that calls that method from outside the package. In other words, treat the warning as signalling the absence of a test case for a public method.

Well, one possible reason could be a redundant class-type.

For instance:

在此输入图像描述

Here, in the above image, "public" keyword is not necessary.

Removing the "public" keyword:

在此输入图像描述

Solves the issue in this simple program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM