如何在java中获得一个常量?

时间:2022-01-28 03:32:13

basically I need to get a constant for a class however I have no instance of the object but only its class. In PHP I would do constant(XYZ); Is there a similar way of retrieving a constant in JAVA?

基本上,我需要为一个类获得一个常量,但是我没有对象的实例,只有它的类。在PHP中,我使用常量(XYZ);在JAVA中有类似的检索常量的方法吗?

I need it to facilitate a dynamic getMethod call

我需要它来促进动态getMethod调用

Class parameterType = Class.forName(class_name);
object.setProperty(field name, field value, parameterType);

the set property method would then get the correct method and set the specified property, however I cant get a method which has int as parameter type without using Interger.TYPE

然后,set property方法将获得正确的方法并设置指定的属性,但是,如果不使用Interger.TYPE,则不能获得具有int作为参数类型的方法

4 个解决方案

#1


1  

I am not sure what you want to get out. But it shouldn't be too difficult to show you an example.

我不知道你到底想要什么。但是给你们举个例子应该不难。

Lets say you have a Class Foo with property bar.

假设您有一个具有属性栏的类Foo。

Class Foo {
    private final String bar = "test";
    public String getBar() {return bar;}
}

Now to get this through reflection you would:

现在要通过反思来得到这个结果,你会:

Class fooClass = Foo.class;
Object fooObj = fooClass.newInstance();
Method fooMethod = fooClass.getMethod("getBar");
String bar = (String) fooMethod.invoke(fooObj);

Now you will get value of method getBar() in bar variable

现在您将在bar变量中获得方法getBar()的值

#2


29  

You might look for sth. like
Foo.class.getDeclaredField("THIS_IS_MY_CONST").get(null); or Class.forName("Foo").getDeclaredField("THIS_IS_MY_CONST").get(null); (thanks f-o-o)

您可以查找。比如Foo.class.getDeclaredField(“THIS_IS_MY_CONST”).get(null);或forname(“Foo”).getDeclaredField(“THIS_IS_MY_CONST”). get(空);(感谢干吗)

Gets the value of a String constant (THIS_IS_MY_CONST) in class Foo.

获取类Foo中的字符串常量(THIS_IS_MY_CONST)的值。

Update use null as argument for get thanks acdcjunior

更新使用null作为get thanks acdcjunior的参数

#3


2  

If this constant is metadata about the class, I'd do this with annotations:

如果这个常量是关于类的元数据,我会用注释来做:

First step, declare the annotation:

第一步,声明注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Abc {
    String value(); 
}

Step two, annotate your class:

第二步,注释你的类:

@Abc("Hello, annotations!")
class Zomg {

}

Step three, retrieve the value:

第三步,检索值:

String className = "com.example.Zomg";
Class<?> klass = Class.forName(className);
Abc annotation = klass.getAnnotation(Abc.class);
String abcValue = annotation.value();
System.out.printf("Abc annotation value for class %s: %s%n", className, abcValue);

Output is:

输出是:

Abc annotation value: Hello, annotations!

#4


0  

Maybe I don't understand what you need, but did you try with final static attributes and static methods?

也许我不明白您需要什么,但是您尝试过最终的静态属性和静态方法吗?

Final means that it can't be changed once set, so you get a constant. Static means it's accessible even if there aren't any objects of the class.

Final意味着一旦设置它就不能被修改,所以你得到一个常数。静态意味着即使类中没有任何对象,它也是可访问的。

#1


1  

I am not sure what you want to get out. But it shouldn't be too difficult to show you an example.

我不知道你到底想要什么。但是给你们举个例子应该不难。

Lets say you have a Class Foo with property bar.

假设您有一个具有属性栏的类Foo。

Class Foo {
    private final String bar = "test";
    public String getBar() {return bar;}
}

Now to get this through reflection you would:

现在要通过反思来得到这个结果,你会:

Class fooClass = Foo.class;
Object fooObj = fooClass.newInstance();
Method fooMethod = fooClass.getMethod("getBar");
String bar = (String) fooMethod.invoke(fooObj);

Now you will get value of method getBar() in bar variable

现在您将在bar变量中获得方法getBar()的值

#2


29  

You might look for sth. like
Foo.class.getDeclaredField("THIS_IS_MY_CONST").get(null); or Class.forName("Foo").getDeclaredField("THIS_IS_MY_CONST").get(null); (thanks f-o-o)

您可以查找。比如Foo.class.getDeclaredField(“THIS_IS_MY_CONST”).get(null);或forname(“Foo”).getDeclaredField(“THIS_IS_MY_CONST”). get(空);(感谢干吗)

Gets the value of a String constant (THIS_IS_MY_CONST) in class Foo.

获取类Foo中的字符串常量(THIS_IS_MY_CONST)的值。

Update use null as argument for get thanks acdcjunior

更新使用null作为get thanks acdcjunior的参数

#3


2  

If this constant is metadata about the class, I'd do this with annotations:

如果这个常量是关于类的元数据,我会用注释来做:

First step, declare the annotation:

第一步,声明注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Abc {
    String value(); 
}

Step two, annotate your class:

第二步,注释你的类:

@Abc("Hello, annotations!")
class Zomg {

}

Step three, retrieve the value:

第三步,检索值:

String className = "com.example.Zomg";
Class<?> klass = Class.forName(className);
Abc annotation = klass.getAnnotation(Abc.class);
String abcValue = annotation.value();
System.out.printf("Abc annotation value for class %s: %s%n", className, abcValue);

Output is:

输出是:

Abc annotation value: Hello, annotations!

#4


0  

Maybe I don't understand what you need, but did you try with final static attributes and static methods?

也许我不明白您需要什么,但是您尝试过最终的静态属性和静态方法吗?

Final means that it can't be changed once set, so you get a constant. Static means it's accessible even if there aren't any objects of the class.

Final意味着一旦设置它就不能被修改,所以你得到一个常数。静态意味着即使类中没有任何对象,它也是可访问的。