O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Discovering Class Fields

Class c = someObject.getClass();
											Field[] publicFields = c.getFields();
											for (int i = 0; i < publicFields.length; i++) {
											String fieldName = publicFields[i].getName();
											Class fieldType = publicFields[i].getType();
											String fieldTypeStr = fieldType.getName();
											System.out.println("Name: " + fieldName);
											System.out.println("Type: " + fieldTypeStr);
											}

You can discover the public fields that belong to a class by using the getFields() method on a Class object. The getFields() method returns an array of Field objects containing one object per accessible public field. The accessible public fields returned do not all have to be fields contained directly within the class you are working with. The following fields are also returned:

  • Fields ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required