Tag: constants

为什么注释字符串值没有实现?

尽管重用了字符串常量和文字,但以下代码段打印出4个不同的哈希码。 为什么字符串值没有插入注释元素? public class Foo { @Retention(RetentionPolicy.RUNTIME) @interface Bar { String CONSTANT = “foo”; String value() default CONSTANT; } public static void main(String[] args) throws Exception { System.out.println(System.identityHashCode(Bar.CONSTANT)); System.out.println(System.identityHashCode(Foo.class.getMethod(“test1”).getAnnotation(Bar.class).value())); System.out.println(System.identityHashCode(Foo.class.getMethod(“test2”).getAnnotation(Bar.class).value())); System.out.println(System.identityHashCode(Foo.class.getMethod(“test3”).getAnnotation(Bar.class).value())); } @Bar public void test1() {} @Bar(“foo”) public void test2() {} @Bar(Bar.CONSTANT) public void test3() {} }

为什么“final static int”可以用作switch的case常量而不是“final static ”

为什么这个int开关有效: public class Foo { private final static int ONE = 1; private final static int TWO = 2; public static void main(String[] args) { int value = 1; switch (value) { case ONE: break; case TWO: break; } } } 虽然这个枚举开关不是: import java.lang.annotation.RetentionPolicy; public class Foo { private final static RetentionPolicy RT = RetentionPolicy.RUNTIME; […]

为什么C#不允许在同一行上使用const和static?

为什么C#不允许在同一行上使用const和static? 在Java中,您必须将字段声明为“static”和“final”以充当常量。 为什么C#不允许你将const声明为final? 我进一步区分在Java中,每个接口都是公共的和抽象的,无论是否显式声明。 const本质上不是有效的静态吗? 为什么C#对此不以为然?

Javadoc使用@value在内部类常量上显示值

我有一个内部类,它声明一个常量,并希望使用@value注释在封闭的顶级类的Javadoc中显示它的值。 例如: /** * {@value #FOO_CONS} // this displays well * {@value #BAR_CONS} // this does not work (checked in the latest Eclipse) * {@value Bar#BAR_CONS} // this does not work, either */ public Foo { public static final int FOO_CONS = 1; static class Bar { public static final int BAR_CONS = 42; } […]

在JSF中有一个“常量”类

我正在使用JSF / Primefaces构建一个Web应用程序。 我需要一个“常量”类,即一个由常量组成的类。 这些常量主要是将在整个应用程序中使用的导航命令。 我这样做的原因是为了避免在ad-hoc基础上实例化字符串。 我如何实现这一点,使得可以从支持bean和XHTML文件访问常量? 我已经尝试使用@ApplicationScoped并使用Singleton模式(Singleton类)但由于范围问题我无法使用它。 或许我只是使用错误的方法。 欢迎任何想法/建议。