51

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 );
        }
    }
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
Jerry Z.
  • 2,031
  • 3
  • 22
  • 28

5 Answers5

84

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.

Andrej Herich
  • 3,246
  • 27
  • 14
75

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

ashario
  • 2,694
  • 1
  • 23
  • 19
  • 8
    This is the practical answer. – Binkan Salaryman Jun 02 '16 at 12:21
  • Normally it is pretty easy to run an Analyze / Inspect Code operation on the module to find out which Lint warning to turn off. This one didn't show up in the report (as of AS 2.2 RC) - and so was harder to find in the endless list of Inspection options. – Jim Andreas Sep 06 '16 at 10:43
  • You can always turn warnings off wherever they appear: (Cursor on warning) -> alt enter -> "warning name" -> disable inspection. This one is pretty useless imho so I always have it turned off. – Cubic Jan 08 '17 at 19:42
23

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

@SuppressWarnings("WeakerAccess")
seekingStillness
  • 4,833
  • 5
  • 38
  • 68
  • 3
    This is also a potentially correct answer if you're providing methods that are going to be used externally to your project (e.g. you're writing code for a framework like me) but don't want to lose the lint warnings for everything else by turning it off in the settings. We do have tests around these methods, but because they're in the same package, the linter is telling us to reduce the method from public to default. – Mykaelos Dec 21 '16 at 18:07
  • 2
    This should be marked the correct answer. When you're dealing with a utility class, this should be suppressed. However, in regular classes, you definitely want this to be present as it enforces good coding practices. – kjdion84 Mar 07 '17 at 23:37
1

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 {
    ...
}
devonlazarus
  • 1,277
  • 10
  • 24
  • In that case he can declare it `private`? – Tom Jun 06 '15 at 04:56
  • @Tom Declaring the class 'private' will prevent it from being seen by other classes in the same package. A class that is declared 'private' can only be seen by the class in which it it declared. Most often used for inner classes. It is most likely the OP wants 'protected'.game – devonlazarus Jun 06 '15 at 07:09
  • I know that. I wonder why you've mentioned `private`, because it is clear that it can't be used here. (repost due to grammar issues) – Tom Jun 06 '15 at 10:27
  • @Tom, there is nothing in his post that tells me how his class is being used. To me, it seemed like an introductory question from a possible new programmer, so I want to be thorough. Note my example demonstrates 'protected' as that is the most likely implementation. Your original question was a great follow up because it allowed for even more explanation. – devonlazarus Jun 07 '15 at 18:04
  • *Idea 16 EAP* marks a class (*not an inner one*) as **Access can be packageLocal**, but when I declare it as ```protected``` it is an error, because that is **Not allowed here**?! I think, one can disable this inspection in the settings, because of its early access state... – Christian Rädel Jan 29 '16 at 15:52
  • 2
    @ChristianRädel The inspection is not buggy, this answer here is wrong. Package-local access is achieved by using no modifier, the `protected` modifier specifies a different access mode. – Rörd Jun 28 '16 at 16:18
  • 1
    @devonlazarus See my previous comment: package-local access is different from protected access. Please change your answer to use no modifier instead of the `protected` modifier. – Rörd Jun 28 '16 at 16:21
0

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.

Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41