83

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?

1
  • 6
    Mine is telling me a method can be package-private, but when I do so, the other places in my code where it's being called won't work (as you'd expect), so why is it telling me it can be package-private? It can't be if I'm using it elsewhere. Not a very helpful suggestion from the IDE here. Commented Jun 13, 2018 at 1:17

5 Answers 5

71

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:

enter image description here

62

To disable on a case by case basis, use

@SuppressWarnings("WeakerAccess")
8
  • 6
    This is the best answer. By doing this you actually document your intention, instead of having the IDE blanket-cover everything for you
    – rath
    Commented Oct 8, 2018 at 10:01
  • 3
    I'd have said declaring a method as public already documented your intention. But I get the point. If you say it twice, then they'll start believing that you really mean it. Commented Jan 31, 2019 at 9:11
  • 2
    It's actually an old anti-pattern to make everything private. TDD users will understand this better than anyone. Essentially, unless you are writing code for distribution and need to protect the internals from you users, making everything private, makes testing and refactoring more difficult. With that in mind, Mayne its not such a bad idea to make it IDE wide. Commented Feb 28, 2019 at 15:15
  • Even then, I'm pretty sure you can configure some obfuscators to change access for you. Commented May 20, 2019 at 22:48
  • 1
    ... Anyway, this discussion goes back at least 20 years, I'm not positive, but I think I first read it in print in Kent Beck's book on TDD. Here are a couple of links to get you started: wiki.c2.com/?CodeSmell wiki.c2.com/?MethodsShouldBePublic I found a few good arguments from the other side as well: carlosschults.net/en/are-private-methods-a-code-smell From experience, I can tell you I've run across more trouble from overly privatized code than anything else, because it starts looking like your bowl of spaghetti internally, where no test can adequately reach. Commented Aug 1, 2019 at 15:44
56

The inspection rule is "Declaration access can be weaker" and there are two options which can be disabled "Suggest package-private visibility level..."

0
8

Move the cursor to public , press Alt + Enter and choose one of the options:
enter image description here

3
  • 2
    Interestingly I don't have the option "Disable inspection" when selecting "Make 'package-private'". (Probably I've disabled another setting so that I don't see that option.) I'm sure your answer works. Thank you. mboss' solution did the trick for me.
    – Thomas
    Commented Jan 18, 2017 at 10:06
  • Worked for me :-)
    – mrek
    Commented Nov 17, 2017 at 17:38
  • 3
    Wouldn't this disable all "Declaration access can be weaker" suggestions, not just the ones suggesting package-private?
    – Joe White
    Commented May 4, 2018 at 22:56
5

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.

2
  • Since it is common that the testing class is in the same package as the class under test, this will not really change anything.
    – Stefan D.
    Commented Jul 11, 2019 at 11:05
  • 1
    @Stefan: Typically unit tests sit in the same package, but a public API should be tested in e.g. integration or acceptance tests and should use the public API as a true client would do. From outside, I think.
    – jazz64
    Commented Oct 3, 2019 at 9:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.