mercredi 23 janvier 2019

"name" field for Integer.class returns null

Class java.lang.Class has private field name. I'm trying to get value of this field for different Class instances, but when it comes to Integer.class the value returned is null. Here's code example:

import java.lang.reflect.Field;

public class Test {

  public static void main(String[] args) throws Throwable {
    Class<Object> objectCls = Object.class;
    Class<Integer> integerCls = Integer.class;
    Class<Class> classCls = Class.class;
    Field nameField = classCls.getDeclaredField("name");
    nameField.setAccessible(true);
    System.out.println(nameField.get(objectCls));
    System.out.println(nameField.get(integerCls));
    System.out.println(nameField.get(classCls));
  }
}

Output will be:

java.lang.Object
null
java.lang.Class

Also I tried executing with online compilers that used JDK 9 and 10 and it was fine there. So problem is when I execute this code with JDK 8.





Aucun commentaire:

Enregistrer un commentaire