Java Reference
In-Depth Information
The reflection-based code has semantically equivalent safety checks, al-
though the checks that are made by the compiler for direct invocation
can be made only at run time when you use invoke . The access checking
may be done in a somewhat different wayyou might be denied access
to a method in your package by the security manager, even if you could
invoke that method directly.
These are good reasons to avoid using this kind of invocation when you
can. It's reasonable to use invoke or the getset methods of Field when
you are writing a debugger or other generic applications that require in-
terpreting user input as manipulations of objects. A Method object can
be used somewhat like a method pointer in other languages, but there
are better toolsnotably interfaces, abstract classes, and nested classesto
address the problems typically solved by method pointers in those lan-
guages.
Exercise 16.7 : Modify your Interpret program to invoke methods on
the object. You should properly display any values returned or excep-
tions thrown.
 
Search WWH ::




Custom Search