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 6007401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:40:33+00:00 2026-05-23T01:40:33+00:00

I am playing with Java’s reflection API, and I’m writing methods that inspect a

  • 0

I am playing with Java’s reflection API, and I’m writing methods that inspect a given object and describes its fields, methods, and so on.

I am using the getFields() method to iterate on the different attributes and display the object contents:

public static void display(Integer i)
{
    System.out.println("An integer: " + i);
}

// Basically a method for each primitive type wrapper

public static void display(Object o)
{
    for (Field f : c.getFields())
    {
        System.out.println("A " + o.getClass() + " which is composed of:");
        display(f.get(o));
    }
}

(Other primitive types and arrays omitted for simplicity’s sake.)

Eventhough Field‘s get method returns an Object, I thought the right methods would be called for the primitive type wrappers (Integers, Strings, etc.), but actually, only display(Object o) is called (no implicit downcasting is performed).

For now, the only solution I have found is to brutally downcast the objects if possible like this:

public static void display(Object o)
{
    if (o instanceof Integer)
    {
        display((Integer) o);
        return;
    }
    else if (o instanceof String 
    {
        ... 
    } // And so on

    for (Field f : c.getFields())
    {
        System.out.println("A " + o.getClass() + " which is composed of:");
        display(f.get(o));
    }
}

This does however seem ugly, and I was wondering if there was a more elegant way to make sure the right method is called.
Any ideas?

Thanks in advance, good people from StackOverflow!

  • 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-23T01:40:34+00:00Added an answer on May 23, 2026 at 1:40 am

    What you want is so called “double dispatch” which Java doesn’t support.

    You can simulate it through reflection: check all your display methods and their argument types, and dynamically invoke one of them based on the runtime type of an object.

    That is unnecessarily clever for this problem. Your if-else chain is just fine.

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

Sidebar

Related Questions

I am playing with Java and want to do a simple while loop that
I'v been playing with java applets recently. In the one im writing now I
I'm currently playing around with java.nio , which I haven't been using for a
I have just started playing with Java, and I really like the language. I
I'm mostly a .Net person at the moment, but I've been playing with Java
Just playing around with java trying to learn it etc. Here is my code
So, I've been working on some playing cards in Java. (Not for any practical
I am making a game that needs a crosshair. I have been playing with
I have been playing with image flood fill that I found here on Stack
I was playing around with the plist of an iPhone project in Xcode, and

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.