Congratulations. You are the first person for ages I have seen post what I think is an advanced question. Off to advanced you go!Originally posted by Roshni Singhania:
Hi,
Sorry if I've posted this in the wrong forum, but I figured this would qualify as the beginner's question.
If the underlying method is static, then the specified obj argument is ignored. It may be null.
Otherwise, the method is an instance method. If the specified object argument is null, the invocation throws a NullPointerException. Otherwise, if the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof) the invocation throws an IllegalArgumentException.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
But when I tried typecasting it, it gives error. I understand that there'd be some more complex syntax for doing all this... but I don't know what. Can anyone please guide me about this?
For the bold part in your comment, as far as I could interpret, it means that the first argument to the method invoke() should be a valid instance of the interface/class or any subclass which is implementing the method which you want to invoke.
For example, if i have another class C and in this call to the method invoke in your code, i pass a reference of C, during run time it will give:
java.lang.IllegalArgumentException: object is not an instance of declaring class
For NullPointerException, make sure that your method on which you are calling invoke and the class reference that you are passing as 1st arg are not null and valid.
Originally posted by Henry Wong:
Unfortunately, in order to cast it (to the full implementation class type), the class must be in scope. And for method inner classes, they are not in scope outside of the method. So, the best you can do is cast it to any interface implemented by inner class, and any super class of the inner class, provided that they are in scope, of course.
Henry
As in, the class of the object passed as the first argument to the "invoke" method" can be a subclass of the actual class/interface, am I right or wrong?
Then what about the second argument? What exactly is the second argument (which you've passed as "null")?
I get "NullPointerException" in both cases and I'm not clear as to what exactly is throwing this exception.
This is an array of arguments which the method you are trying to call, expects i.e. its formal parameters. In your case, there was no parameter, so null. Check the )]javadocfor complete explanation.
quote:
I get "NullPointerException" in both cases and I'm not clear as to what exactly is throwing this exception.
This is because you are calling goMLI() which is void and expecting an int while assigning to your integer variable.
Originally posted by amitabh mehra:
Apart from this, I tried serializing and deserializing seperately i.e, in two different runs. And during second run I also changed the value of the final int i in method go(). But the output showed the new changed value when it should have shown the one that my de-serialized object had (if it had any??).
I tried the following code in class MLI:
and again did serialization and de-serialization sepreately. In readObject() it printed the correct de-serialized value. But integer returned from invoke method was the new value.
Sorry, I am lost on this . Can someone help on this?
[ May 26, 2008: Message edited by: amitabh mehra ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |