路径适配器声明常量报错
Modifier 'public' not allowed here
Modifier 'static' not allowed here
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
是照着写的,人家那就是正常的,org.springframework.util.AntPathMatcher 库也正常导入了
路径适配器声明常量报错
Modifier 'public' not allowed here
Modifier 'static' not allowed here
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
是照着写的,人家那就是正常的,org.springframework.util.AntPathMatcher 库也正常导入了
在 Java 中,常量必须被声明为 public static final 三个修饰符的组合。在你提供的代码中,错误信息是因为你将 public 和 static 这两个修饰符分别放在了不同的位置。正确的写法应该是将它们放在同一个位置,即在常量声明前加上 public static final,如下:
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
这样声明后,你就可以在任何类中使用 PATH_MATCHER 常量了。
如果你需要在类中声明多个常量,你可以将它们放在一个代码块中,如下所示:
public class MyClass {
public static final int MAX_NUMBER = 100;
public static final String APP_NAME;
static {
APP_NAME = "My App";
}
}
在这个例子中,常量 MAX_NUMBER 和 APP_NAME 都被声明为 public static final,APP_NAME 的值是在静态代码块中赋值的。