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
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!