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

The Archive Base Latest Questions

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

Possible Duplicate: Java Class.cast() vs. cast operator I am unsuccessfully trying to find out

  • 0

Possible Duplicate:
Java Class.cast() vs. cast operator

I am unsuccessfully trying to find out what Class.cast() does or what it may be good for. At the same time I am wondering whether I can somehow cast an object via reflection.

First I thought something like the lines below might work:

Object o = "A string";
String str = Class.forName("java.lang.String").cast(object);

But without an explicit cast it does not work.

So what is the cast method of Class class good for? And is it somehow possible just with reflection to cast objects, so you find the object’s class, use Class.forName on it and cast it somehow?

  • 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:37:02+00:00Added an answer on May 28, 2026 at 5:37 am

    An example where is does work:

    class Favorites {
        private Map<Class<?>, Object> map = new HashMap<Class<?>, Object>();
    
        public <T> T get(Class<T> clazz) {
            return clazz.cast(map.get(clazz));
        }
    
        public <T> void put(Class<T> clazz, T favorite) {
            map.put(clazz, favorite);
        }
    }
    

    which allows you to write:

    Favorites favs = new Favorites();
    favs.put(String.class, "Hello");
    String favoriteString = favs.get(String.class);
    

    The reason your code doesn’t work is that Class.forName() returns a Class<?>, i.e. a class object representing an unknown type. While the compiler could possibly infer the type in your example, it can not in general. Consider:

    Class.forName(new BufferedReader(System.in).readLine())
    

    what’s the type of this expression? Clearly the compiler cannot know what the class name will be at runtime, so it doesn’t know whether

    String s = Class.forName(new BufferedReader(System.in).readLine()).cast(o);
    

    is safe. Therefore it requests an explicit cast.

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

Sidebar

Related Questions

Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: In Java, what does a reference to Class.class do? Firstly apologies for
Possible Duplicate: Dynamically loading a class in Java hi i am trying to create
Possible Duplicate: Retain precision with Doubles in java import static java.lang.System.out; public class q2{
Possible Duplicate: java operator ++ problem public class A { public static void main(String[]
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: Java inner class and static nested class What are the uses of
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
Possible Duplicate: Java's Interface and Haskell's type class: differences and similarities? When I started

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.