Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:09:23+00:00 2026-06-17T15:09:23+00:00

I have a class name (as a String ), and I want to get

  • 0

I have a class name (as a String), and I want to get all members and their types. I know I need to use reflection, but how?

For instance, if I have

class MyClass {
    Integer a;
    String b;
}

how do I get the types and names of a and b?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:09:25+00:00Added an answer on June 17, 2026 at 3:09 pm

    If the classes have already been loaded by the jvm, you can use the static method of Class called Class.forName(String className); and it will return you a handle to the reflections object.

    you would do:

    //get class reflections object method 1
    Class aClassHandle = Class.forName("MyClass");
    
    //get class reflections object method 2(preferred)
    Class aClassHandle = MyClass.class;
    
    //get a class reflections object method 3: from an instance of the class
    MyClass aClassInstance = new MyClass(...);
    Class aClassHandle = aClassInstance.getClass();
    
    
    
    //get public class variables from classHandle
    Field[] fields = aClassHandle.getFields();
    
    //get all variables of a class whether they are public or not. (may throw security exception)
    Field[] fields = aClassHandle.getDeclaredFields();
    
    //get public class methods from classHandle
    Method[] methods = aClassHandle.getMethods();
    
    //get all methods of a class whether they are public or not. (may throw security exception)
    Method[] methods = aClassHandle.getDeclaredMethods();
    
    //get public class constructors from classHandle
    Constructor[] constructors = aClassHandle.getConstructors();
    
    //get all constructors of a class whether they are public or not. (may throw security exception)
    Constructor[] constructors = aClassHandle.getDeclaredConstructors();
    

    To get a variable named b from MyClass, one might do.

    Class classHandle = Class.forName("MyClass");
    Field b = classHandle.getDeclaredField("b");
    

    and if b is type integer, to get its value i would do.

    int bValue = (Integer)b.get(classInstance);//if its an instance variable`
    

    or

    int bValue = (Integer)b.get(null);//if its a static variable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like that: public class Customer { public string Name {get;
I have an abstract class as follows. I want to get all the values
I have class student : public class Student { public string Name { get;
I have a domain class: class Author { String name static hasMany = [superFantasticAndAwesomeBooks:
I have the following class structure: class Organization { string Name; List<User> users; List<Organization>
i have a class: public class Vars { public static final String NAME =
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
In c# you can have public class Foo { public Foo(string name) { //do
I have the following Grails domain class: class Product { String name Float basePrice
Say I have a structure like: class SomeObject Public Name as String Public Created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.