-4

I'm wondering how I would get a class from a java.lang.Object object to a specific class, such as a Cat class I may write; not directly casting to Cat, but to automatically get the main class of the object and cast to the main class, since I may take in Object arguments to take in many kinds of classes. Would getting an object through Class<T>, getting T (the type of the main class) be possible?

Here's what I mean:

class Cat {
    public String meowMessage = "meow";

    public void meow() {
        System.out.println(meowMessage);
    }
}

public class Main {
    public static void main(String[] args) {
        Object obj = new Cat();
        // Cat c = obj.getClass()...;
        // ^ how to get the Class object and automatically cast to the main class (Cat)?
    }
}
Macintosh Fan
  • 355
  • 1
  • 4
  • 18
  • 1
    if you know you need a `Cat`, cast or just declare the variable as `Cat`. If you don't know, what are you going to do with it? – njzk2 Aug 17 '21 at 19:47
  • To be clear, is there any method in the Class class where I can directly get the main type of the class? Class the instance of T? – Macintosh Fan Aug 17 '21 at 19:49
  • Yes, it's called `getClass`, which you use in your commented code up above. That returns a `Class>`. But what do you want to do with that? Because that's a class object, not a `Cat`. – Silvio Mayolo Aug 17 '21 at 19:51
  • Does this answer your question? [Downcasting in Java](https://stackoverflow.com/questions/380813/downcasting-in-java) – Gaël J Aug 17 '21 at 19:51
  • Can I get a Cat object from a Class object? – Macintosh Fan Aug 17 '21 at 19:51
  • Well you can call `clazz.getDeclaredConstructor().newInstance()`. That will use a parameterless constructor if there is one. But what do you want to do if there isn't one? Your question is very unclear at the moment. – Jon Skeet Aug 17 '21 at 19:56
  • [w.l.o.g.](https://en.wikipedia.org/wiki/Without_loss_of_generality) no. The class could have an arbritrary complex constructor, which we might not be able to call in an automated way. – Turing85 Aug 17 '21 at 19:56
  • @JonSkeet Well, I guess the one issue with my question is that the constructor may be different. – Macintosh Fan Aug 17 '21 at 19:58
  • Therefore, my question may not be possible in Java? – Macintosh Fan Aug 17 '21 at 19:59
  • 1
    Your question is too vague for us to tell whether what you want to do is possible or not. If you make the question *much, much* clearer, we may be able to help you. The lack of clarity is the issue with your question - note that in the body of the question you don't *mention* trying to create a new instance of the class... – Jon Skeet Aug 17 '21 at 20:04
  • Ok I edited the question. Hopefully, it's a little more clear now. – Macintosh Fan Aug 17 '21 at 20:07
  • if i got you then @j –  Aug 17 '21 at 20:08
  • 2
    What you want is not possible. Especially since it would involve to determine a generic parameter at runtime. Due to [type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html), this is not possible. – Turing85 Aug 17 '21 at 20:09
  • @Turing85 May as well leave an answer before the question is closed. – Macintosh Fan Aug 17 '21 at 20:10
  • @MacintoshFan [there you are](https://stackoverflow.com/a/68823454/4216641) – Turing85 Aug 17 '21 at 20:11
  • if i got you then @JonSkeet already answered you use Cat.class.getDeclaredConstructor().newInstance() or new Cat().getCLass.getDeclaredConstructor().newInstance() https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...) or Cat.class.getDeclaredConstructors() which return array of constructor then use one to call newInstance() on it or new Cat().getCLass.getDeclaredConstructors() same as above –  Aug 17 '21 at 20:15
  • 3
    Nope, it's really still not clear, because you haven't answered the very first comment: what do you want to *do* with this once you've got it? You don't know the type of it (otherwise you could write that in code) so what are you trying to do? At the moment this question is an XY problem at best. – Jon Skeet Aug 17 '21 at 20:16

2 Answers2

2

What you want would involve determining a generic parameter at runtime. This is not possible due to type erasure.

Turing85
  • 18,217
  • 7
  • 33
  • 58
1

If I understand correctly, you need instanceof so you can write:

if (obj instanceof Cat) {
  Cat cat = (Cat) obj;
  cat.meow();
} else {
  //do something else
}

It's not possible to cast to a type which is not known compile time and I don't think it would make any sense because casting is needed to access the members of the class instance statically from the code. But how to know what is accessible if the class is unknown?

Szellem
  • 484
  • 2
  • 11