jeudi 7 janvier 2016

Can you cast without assignment to another object?

In java is it possible to cast a object without having to assign it to another object. For example:

Object x = Class.forName(name).newInstance();
if(x instanceof Date){
    (Date)x; 
    x.setTime(0);
}

The compiler doesn't let me do that unless I assign it to another Date object, but is there a way in which you can cast without having to assign to another object? Because when you assign for example Date a = (Date)x, a and x end up being references, both being able to touch the Date methods. But to make my code cleaner, is there a way where i can cast without having to assign it?





Aucun commentaire:

Enregistrer un commentaire