Is there any way in java to store the type of an object in a variable? Like if I wanted to get a class type as a parameter and then return the correct type of object.
Asked
Active
Viewed 2,830 times
0
-
2https://docs.oracle.com/javase/tutorial/java/generics/ – Mohammed Aouf Zouag Dec 04 '15 at 00:27
-
What are you *really* trying to accomplish that can't be accomplished with the use of the factory pattern? – Makoto Dec 04 '15 at 00:39
-
There's also a class called [`Class`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html). The methods [`newInstance()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#newInstance--) and [`cast()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#cast-java.lang.Object-) might be handy, but it's hard to tell from a description this vague. Post some "code" that at least outlines what, exactly, you're trying to do. – millimoose Dec 04 '15 at 00:44