This question has already been answered:

Suppose there is a class with tens / hundreds of fields. There are getters and setters. It is necessary to display the names of all fields. Let be:

int var = 666; String field = "El Diablo"; 

In the console you need to get: var field and all! Well, at the very least , getVar getField names , for example, will disappear .

In general, ideally, I would also use the types of variables to display on the screen in a simple way, otherwise I had to write a separate TypeOfVar class .

Reported as a duplicate by participants Alexey Shimansky , user194374, Alex , aleksandr barakin , pavel 1 Dec '16 at 11:48 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Somewhere here: docs.oracle.com/javase/tutorial/reflect - Vartlok
  • Thanks for Parsing, if I knew that it exists, I would not ask a question. - Jürgen von Markoff
  • @ JürgenvonMarkoff don't forget to read a little more about reflection (reflection) - Alexey Shimansky
  • @ Alexey Shimansky read! Thanks again! - Jürgen von Markoff

1 answer 1

Like that

 //Получаем список полей класса А Field[] declaredFields = A.class.getDeclaredFields(); //перебираем for (Field field : declaredFields) { //Выводим имя поля System.out.println(field.getName()); //Выводим тип поля System.out.println(field.getType()); }