17

I made function 'warn' in line 17 whose parameter is enum Shape. Why is it warning about visibility scope and how can I fix it?

import java.util.Scanner;

public class AreaCalculator {

    enum Shape {TRIANGLE, RECTANGLE, CIRCLE}
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String str = scanner.next();

        while (!str.equals("quit")){
            str = str.toUpperCase();
            warn(Shape.valueOf(str));
        }
    }

    public static void warn(Shape shape) { //warning

    }

IntelliJ recommends generate overloaded method with default parameter values like following code.

public static void warn(){
    warn(null);
}

But I think it doesn't look intuitive.

3
  • 1
    Make enum Shape public, or warn not public.
    – user8681
    Commented Sep 18, 2023 at 8:44
  • Make Shape public, or make warn package-private (or private).
    – Slaw
    Commented Sep 18, 2023 at 8:44
  • You've learned something important, when a static analysis tool like IntelliJ (or SonarQube) suggests something, it might remove the warning, but in a really stupid way.
    – Kayaman
    Commented Sep 18, 2023 at 8:50

2 Answers 2

28

Why is there a warning Class 'Shape' is exposed outside its defined visibility scope?

Because the enum AreaCalculator.Shape is only visible to classes in the same package, but the method public static void warn(Shape shape) is visible to any class.

So if we write a class:

package a;

import b.AreaCalculator;

public class AreaCalculatorClient {
    public static void main(String[] args) {
        AreaCalculator.warn(AreaCalculator.Shape.CIRCLE);
    }
}

It will fail to compile, because 'b.AreaCalculator.Shape' is not public in 'b.AreaCalculator'. Cannot be accessed from outside package.

The fix is to with make Shape public or warn package-private, depending on your intent.

The fix suggested by IntelliJ IDEA is something you might do if you're convinced that you've chosen the correct visibility for Shape, and yet you want to call something like the warn method from arbitrary classes.

2
  • I love your answer! thank u :)
    – hong
    Commented Sep 18, 2023 at 9:01
  • 4
    @hong: there's a dedicated button meaing "love this answer", which is the upvote button, you should probably press that (in fact that's preferable to posting it as a comment). Additionally, if it helped to solve your problem, feel free to accept the question as well. Commented Sep 18, 2023 at 9:05
-1

"... I made function 'warn' in line 17 whose parameter is enum Shape. Why warning about visibility scope and how can I fix it? ..."

I'm not receiving any warning here, the code compiles.
If I enter any of the values I get an infinite loop.

To resolve this, assign str upon each while-loop iteration.

String str;

while (!(str = scanner.nextLine()).equals("quit")){
    str = str.toUpperCase();
    warn(Shape.valueOf(str));
}

Output

TRIANGLE
TRIANGLE
RECTANGLE
RECTANGLE
CIRCLE
CIRCLE
quit

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