Does using (Class) object reinstate the object in java?

Viewed 74

If I were to use something like:

Class object = (Class) object;

would that reinstate the object with the current properties or would it be the same object under another class? I want to make sure I'm not reinstating the same thing over and over because I'm going to use code like this in a render method.

2 Answers

It would be helpful if you provided more of a context as to what you are trying to achieve. I think what you have written now would throw a "Variable 'object' is already defined in the scope" error.

If you are trying to cast object to type Class, you would need to use a different variable name such as:

Class object2 = (Class) object;

This does not create or re-instantiate anything, the variable object2 still points to the same object.

For a more extensive answer on how casting is handled:

How does Java Object casting work behind the scene?

No new instance is created in your example. You're just casting it's type into a different type.

Related