Originally posted by Salmon Chase:
...if (o.getClass().getName().equals("Report")) {...
Will the conditional statement work...
Yes, and you can add some println statements to your code to demonstrate this. But it might be easier (and more efficient!) to use the
instanceof operator...
if(o instanceof Report) {...
Originally posted by Salmon Chase:
...Report report = new Report();
report = o; //This probably doesn't work...
No, it doesn't, and the error message tells you why.
...incompatible types
found : java.lang.Object
required: Report...
Since you're downcasting (from Object to Report), you need an explicit cast. Indeed, this is why the conditional
test is a good idea -- to verify what you have before attempting a cast that might otherwise cause a RuntimeException.
Also, it doesn't make much sense to create a new object only to reassign the variable in the next line. This just creates a new instance that's immediately ready for Garbage Collection.
[ August 10, 2006: Message edited by: marc weber ]