Why would I use java.lang.Class.cast [duplicate]
This question already has an answer here: When should I use the java 5 method cast of Class? 5 answers Java Class.cast() vs. cast operator 8 answers I recently stumbled upon a piece of code that went like this: Object o = .. ; Foo foo = Foo.class.cast(o); I was actually not even aware that java.lang.Class had a cast method, so I looked into the docs, and from what I gather this does simply do a cast to the class that the Class object represents. So the code above would be roughly equivalent to Object o = ..; Foo foo = (Foo)o; So I wondered, why I would want to use the cast method instead of