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

JavaIntellij IdeaInspection

Java Problem Overview


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

  Problem synopsis      Can be package local (at line 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 );
        }
    }
}

Java Solutions


Solution 1 - Java

IDEA is referring to package-private visibility.

> 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

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

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

Solution 2 - Java

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

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

Solution 3 - Java

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

@SuppressWarnings("WeakerAccess")

Solution 4 - Java

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.

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 {
    ...
}

Solution 5 - Java

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJerry Z.View Question on Stackoverflow
Solution 1 - JavaAndrej HerichView Answer on Stackoverflow
Solution 2 - JavaasharioView Answer on Stackoverflow
Solution 3 - JavaseekingStillnessView Answer on Stackoverflow
Solution 4 - JavadevonlazarusView Answer on Stackoverflow
Solution 5 - JavaWahib Ul HaqView Answer on Stackoverflow