vendredi 13 avril 2018

How to get field name in JAVA

in code i have soemthing like:

 public static final String DOG= "dog";
 public static final String CAT= "cat";

 String dog = "soome value";
 String dog = "other value";
 //To avoid this construction:
  method(dog, DOG);
  method(cat, CAT);

I prefer not to declare for each field special static string with has the same name, that field have, like:

method(dog, dog.getFieldName());
method(cat, cat.getFieldName());

Is there some way to do this?

EDIT: I try to avoid multiple declaration of static field, used only one time. This fields has the same name as value of static string, with are made to send this name of field to database.

Im using the method to check in database if its posible to store value, so I have a string value, with hase diferent sizes. And I also have field name with is the same in database, java code and in html form. So I dont need to declare this as static and i wonder if it is possible to get just this name how it is named in java code

EDIT2: I tried to do it by reflection but it not works:

method(dog, MyClass.class.getField(dog).getName());
method(cat, MyClass.class.getField(cat).getName());





Aucun commentaire:

Enregistrer un commentaire