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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:23:14+00:00 2026-05-13T14:23:14+00:00

I imagine that there has to be some way to use reflection to do

  • 0

I imagine that there has to be some way to use reflection to do what I want to do.

I need to be able to take a string at runtime that is of a certain class, for example:

string s = "mypackage.MySuperClass"

Then I may have an object of some type. It could be one of the following:

mypackage.MySuperClass obj = new mypackage.MySuperClass();

or

mypackage.MySubClass obj2 = new mypackage.MySubClass();

or

someotherpackage.SomeOtherClass obj3 = new someotherpackage.SomeOtherClass();

What I need to do is see if an object (which its type is determined at runtime), is equal to the string s (which is also determined at runtime via completely different means).

In the cases above I would want obj and obj2 to be the same type as s (since MySubClass is a subclass of MySuperClass), and obj3 would not.

Is there an easy way to do this in java? Possibly something using instanceOf?

  • 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-13T14:23:14+00:00Added an answer on May 13, 2026 at 2:23 pm

    Sounds like you want something like this:

    boolean isInstance(Object o, String className) {
        try {
            Class clazz = Class.forName(className);
            return clazz.isInstance(o);
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }
    

    Or you could do it the other way round – take o’s class (o.getClass()), find all ancestor classes and compare their names to className.

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

Sidebar

Related Questions

Is there a way to use DecimalFormat (or some other standard formatter) to format
Imagine you have an app that manages some library and has its own data.
I would like to ask you: Imagine that you have a site and there
Imagine this scenario: in our company there is an employee that play around graphic,css,html
Good morning all. Is there any jsf control that escapes the html tags? Imagine
Imagine that I want to create an array from another array like this: $array
I know that each programming language has certain guideline and styles. My question is
First off, I want to say there is no use case for this. The
Imagine that I have a form in a flash application with two fields, input1
Imagine that during a foreach(var item in enumerable) The enumerable items change. It will

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.