Вопрос

Regarding the example code below, although the Test() constructor inside the class Test is public, the class Test itself isn't public, and so the Test() constructor can't be called from outside its own package.

Does that make the public keyword redundant? If so, I wonder why javac doesn't issue a warning about the redundant use of public, when used inside a class whose access is implicitly declared as default ("package private")?

Test.java, package test -

package test;
class Test {
    public Test() {}
}

Main.java, package main -

package main;
class Main {
    public static void main(String[] args) {
        new test.Test();  // Expected error
    }
}

EDIT:

Just to be clear: it's when I compile Test.java, that I get no warning.

Это было полезно?

Решение

The user comments made to my original post have helped me solve this problem: I now realise that an IDE will give me the information that I was after, and that just using javac on its own will not. Thanks.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top