java get object

Solutions on MaxInterview for java get object by the best coders in the world

showing results for - "java get object"
Camilo
25 Jan 2020
1package com.tutorialspoint;
2
3import java.lang.annotation.Retention;
4import java.lang.annotation.RetentionPolicy;
5import java.lang.reflect.Field;
6
7public class FieldDemo {
8
9   public static void main(String[] args) throws NoSuchFieldException, 
10      SecurityException, IllegalArgumentException, IllegalAccessException {
11          
12      SampleClass sampleObject = new SampleClass();
13      sampleObject.setSampleField("data");
14      
15      Field field = SampleClass.class.getField("sampleField");
16      System.out.println(field.get(sampleObject));
17   }
18}
19
20@CustomAnnotation(name = "SampleClass",  value = "Sample Class Annotation")
21class SampleClass {
22
23   @CustomAnnotation(name="sampleClassField",  value = "Sample Field Annotation")
24   public String sampleField;
25    
26   public String getSampleField() {
27      return sampleField;
28   }
29
30   public void setSampleField(String sampleField) {
31      this.sampleField = sampleField;
32   }
33}
34
35@Retention(RetentionPolicy.RUNTIME)
36@interface CustomAnnotation {
37   public String name();
38   public String value();
39}