0

when @RequestMapping used a static final constan in parent class which is not in same package with subClass class,the idea give me a error like

'V1_BASE_URL' has protected access in 'XXXXXXXX.BaseController'

so,i dont know why,the next is my code.

this is the parent class

package com.controller;
public class BaseController {

protected static final String V1_BASE_URL = "/crm/v1/";

}

this is subclass,and it is not in the same package with BaseController

package com.controller.corpus;
@RequestMapping(V1_BASE_URL+"/crm/v1/corpus_subset")
public class CorpusSubsetController extends BaseController {

}

error image

i dont find some useful answer can deal my problem,but i found if the subclass and father in the same package,the annotation in classname can use static constant,but if the subclass not in the same package with father, the annotation can not use,so my question is the annotation in classname belongs who?if the annotation in a class which one in the package A,so the annotation belongs the package A??

威先森
  • 1
  • 1
  • 1
    This is a duplicate question and has been answered here [link](https://stackoverflow.com/questions/24289070/why-we-should-not-use-protected-static-in-java) – prashant.kr.mod Jul 21 '20 at 09:32
  • If it is not in the same package, you can't access protected attributes by design so this is expected. – T A Jul 21 '20 at 09:34
  • 2
    Does this answer your question? [What is the difference between public, protected, package-private and private in Java?](https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in) – T A Jul 21 '20 at 09:34
  • 1
    see https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – chaos Jul 21 '20 at 09:36

1 Answers1

0

It's because you are trying to access V1_BASE_URL in the annotation which is outside the class scope. If you access it like this you won't have any compilation errors for example :

public class CorpusSubsetController extends BaseController {
    public void test() {
        String test = V1_BASE_URL;
    }
}
Fabien
  • 974
  • 9
  • 14
  • ok,thanks,but i also have question, when the subclass with father in the same package,the V1_BASE_URL can used in the annotation @RequestMapping,this annotation is outside the class scope too. – 威先森 Jul 22 '20 at 02:07