java - Getting a Field from an instance? -


is there way field reference instance (not class) ?

this example :

public class element {     @myannotation("hello")     public string label1;      @myannotation("world")     public string label2; }  public class app {     private element elem = new element();      public void printannotations() {         string elemlabel1 = elem1.label;         string elemlabel2 = elem2.label;      // cannot elemlabel.getfield().getdeclaredannotations();         string elemlabel1annotationvalue = // how ?          string elemlabel2annotationvalue = // how ?      } } 

sorry not being clear, know how fetch fields class (class --> field --> declaredannotations)

what wondering how field particular instance. in example, elemlabel1 string instance, wish able field of element.label1.

what mean? field on defined on class. can value specific instance:-

private static class test {     private int test = 10; }  public static void main(string[] args) throws exception {     final test test = new test();     final field field = test.class.getdeclaredfield("test");     field.setaccessible(true);     final int value = field.getint(test);     system.out.println(value); } 

the class test has field called test. true of test - defined in class. instance of class has specific value field, in case 10. can retrieved specific instance using getxxx or get method.

edit

from code in question looks want value of annotation field not value of class field.

in java, values in annotations compile time constants , therefore defined @ class rather instance level.

public class element {     @myannotation("l")     public string label; } 

in example, myannotation value field must equal 1 every instance of element.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -