• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Get Fields using Reflection

March 16, 2014 //  by Krishna Srinivasan//  Leave a Comment

In this example we shall explain with a simple example to get the fields of a class using the reflection. You can list the public and private fields of an object using the following steps:

  • Get the Class instance of the class which you want to list the fields. In this example get the Class instance of Math class.
  • Call getFields() method from the Class instance which returns the Field[] array which contains the public fields.
  • Call getDeclaredFields() method from the Class instance which returns the Field[] array which contains all the fields whether it is private, public or default.

Lets look at this example to list of fields of java.lang.Math class:

[code lang=”java”] package javabeat.net.reflection;

import java.lang.reflect.Field;

public class JavaBeatReflectionExample {

public static void main(String[] args) throws Exception {

// Get the Class instance for Math
Class<?> mathClass = java.lang.Math.class;

//Declare Fields class
Field[] refFields;

// List all the public fields in the Math class
refFields = mathClass.getFields();
for (int i = 0; i < refFields.length; i++) {
System.out.println("Public Field: " + refFields[i]);
}

System.out.println();

// List all the fields in the Math class
refFields = mathClass.getDeclaredFields();
for (int i = 0; i < refFields.length; i++) {
System.out.println("Field: " + refFields[i]);
}

}
}
[/code]

Output

[code] Public Field: public static final double java.lang.Math.E
Public Field: public static final double java.lang.Math.PI

Field: public static final double java.lang.Math.E
Field: public static final double java.lang.Math.PI
Field: private static java.util.Random java.lang.Math.randomNumberGenerator
Field: private static long java.lang.Math.negativeZeroFloatBits
Field: private static long java.lang.Math.negativeZeroDoubleBits
[/code]

Category: JavaTag: Core Java, Java Reflection

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Invoke Method using Reflection API
Next Post: Get Methods using Reflection »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact