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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:43:12+00:00 2026-05-23T04:43:12+00:00

I must be missing something here, but I seem to be having some trouble

  • 0

I must be missing something here, but I seem to be having some trouble doing some basic reflection. I thought that due to things like boxing that I would receive true for each of the two prints below. Here is the simple Main class:

package com.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestingReflection {

    public static void main(String[] args) throws SecurityException,
                                                  NoSuchMethodException,
                                                  IllegalArgumentException, 
                                                  IllegalAccessException,
                                                  InvocationTargetException 
    {
        final Class c = Reflection.class;
        Reflection p = new Reflection();
        p.setIntObj(new Integer(1));
        p.setIntPrim(1);
        for (Field field : c.getDeclaredFields()) {
            char first = Character.toUpperCase(field.getName().charAt(0));
            String capitalized = first + field.getName().substring(1);
            Method getField = 
                  c.getDeclaredMethod("get" + capitalized, new Class [] {});
            Class fieldClass = getField.getReturnType();
            Method setField = 
                  c.getDeclaredMethod("set" + capitalized,
                                      new Class [] { fieldClass });
            Object value = getField.invoke(p, new Object [] {});
            if (value != null) {
                System.out.println("Field Class: " 
                                    + fieldClass.getName() 
                                    + " instanceOf: " 
                                    + fieldClass.isInstance(value) 
                                    + " Value Class: " 
                                    + value.getClass().getName());
            }
        }
    }
}

Here is the class I am running it against:

package com.reflection;

public class Reflection {

    private int intPrim;
    private Integer intObj;
    public int getIntPrim() { return intPrim; }
    public void setIntPrim(int intPrim) { this.intPrim = intPrim; }
    public Integer getIntObj() { return intObj; }
    public void setIntObj(Integer intObj) { this.intObj = intObj; }
}

Here is the output I receive:

Field Class: int instanceOf: false Value Class: java.lang.Integer
Field Class: java.lang.Integer instanceOf: true Value Class: java.lang.Integer

Is there a different method I should be using to determine this? isAssignableFrom also returns false for the primitive.

  • 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-23T04:43:13+00:00Added an answer on May 23, 2026 at 4:43 am

    The simplest way to get the primitive type for int is int.class You can also use Integer.TYPE but I believe this is there for backward compatibility.

    The Java doc Class.isIntanceof(Object) says

    If this Class object represents a primitive type, this method returns false.

    This is perhaps not a useful definition, but it is the way it works. I haven’t found a simple way to get the wrapper class for a primitive type so I use a predefined HashMap (it has 9 classes, including void)

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

Sidebar

Related Questions

I must be missing something simple here, but I'm having trouble retrieving data from
This seems very basic and I must be missing something, but here goes anyways...
I must really be missing something obvious, but I'm having trouble with general use
I know I must be missing something here, but I cannot seem to get
I must be missing something obvious here, but I can't seem to get the
I know SQL well but I must be missing something really dumb here. This
Now I must be missing something here, as this seems a very basic issue
I must be missing something totally obvious but I can't seem to get the
Must be missing something here, but I'm using Node_redis as Node.js client for Redis.
I must be missing something, silly, but here is the problem. I have a

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.