Search code examples
javaooppublicaccess-specifier

method defined in class inaccessible


I was following this example: https://www.boraji.com/spring-boot-configurationproperties-example To make a nested list of Java Spring properties, that, obfuscated and simplified, looks like this:

conf.property:

a.b.c=item1,item2,item3

AppProperties.java (located in package x.y.z.properties):

@EnableConfigurationProperties
@ConfigurationProperties(prefix = "a")
@Configuration
public class AppProperties {
    private String version;
    private String email;
    public BProperties b = new BProperties();



    static class BProperties {
        public List<String> c;

        public List getC() {
            return c;
        }

        public void setC(List c) {
            this.c= c;
        }
    }
}

FieldValidator.java (located in package x.y.z.validation):

public class IsItemValidator implements ConstraintValidator<IsItem, Object> {

    @Autowired
    public AppProperties appProperties;
    //... bunch of other stuff...

    private boolean hasValidItem(final Object item) {


        return appProperties.getB().getC().contains(item);
    }
}

For simplicity I did not write all the getters/setters, they are there, and are public everything.

I still get:

java: getC() in a.b.c.properties.BProperties is defined in an inaccessible class or interface

I tried googling and looking at similar questions, but none of the answers make sense to me.. What is going on here? I already made everything public as per one of the answers I read regarding different packages..


Solution

  • Declare your BProperties class as public:

    public static class BProperties {
    

    In your code it has package-private visibility, so IsItemValidator class can't see it because it is located in another package.