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

  • SEARCH
  • Home
  • 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 7073385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:24+00:00 2026-05-28T05:55:24+00:00

Im going round in circles on this. I have a class Person, eg public

  • 0

Im going round in circles on this.

I have a class Person, eg

public class Person {
String name = "";
}

Now, I would like to introspect this class instance & figure out what Class is name declared as.

So, name = String or java.lang.String

This is my Code:

‘this’ is an instance of Person.

try {
    String className = this.getClass().getName();
        Class cls = Class.forName(className);
        Field fieldlist[] = cls.getDeclaredFields();
        for (int i = 0; i < fieldlist.length; i++) {
           Field fld = fieldlist[i];
           int mod = fld.getModifiers();
           System.out.println("1. " + fld.toGenericString());
           System.out.println("2. " + fld.getName());
           System.out.println("3. " + fld.getGenericType() + "]");



           Object oj = fld.getType();

           // Says that 4: class java.lang.String

           System.out.println("4: " + oj.toString());
           Class c1 = oj.getClass();

           // Should throw Exception
           String stype = c1.getDeclaringClass().toString();
           System.out.println("5. " + stype);


        }
      }
      catch (Throwable e) {
         System.err.println(e);
      }

I managed to get to a part that states:

class java.lang.String

but I need it to be “java.lang.String”

Any ideas?

  • 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-05-28T05:55:24+00:00Added an answer on May 28, 2026 at 5:55 am

    Try.. getType() and then getName()

     fld.getType().getName()
    

    Edit:(Aften Green Days’ comment) — Note that
    fld.getType().getCanonicalName() will give same output in most cases. The output is different when innerclasses are used. Here is link came from search. Depending what you need to do with classname you may choose one of getName() or getCanonicalName()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Weird question this, going round in circles. I have 2 pages. Page 1. Has
Going round in circles here i think. I have an activity called Locate; public
I have been going round in circles with this. I am hoping someone can
I keep going round in circles with this one. I have managed to set
Something really odd going on here, and I have gone round in circles trying
I seem to be going round in circles. I have a WPF application that
I've been going round in circles with this for a couple of days, and
I need some help here, I'm currently going round in circles. I have a
I feel like i'm going round in circles here and missing something really daft...
I have been going round in circles trying to extract meta tag information from

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.