What is the difference between the invocation and execution of a method ? Are two the same thing ?
I don't think these are standard terms. However I understand them the following way:
- invocation is the event of issuing the call to the method; technically - placing the method onto the stack
- execution is the whole process of running the method - from invocation till completion. And execution time is period during which the method body runs.
Well, invoking a method means calling it by its name and parameters; executing a method means executing it.. running it, fetching its lines one by one and run them.
There are some subtle differences:
- Context
- An invocation context is associated with the caller
- e.g. the parameters you're using to invoke a method are the actual parameters
- An execution context is associated with the callee
- e.g. the parameters you're using in a method execution are formal parameters
- An invocation context is associated with the caller
- Dynamic dispatch
- A method invokation can lead to the execution of any one of many methods
- An executing method is precisely one executing method
- Order: invocation precedes execution
- Invocation of a method doesn't immediately start its execution
- Imagine if the method is remote
- Invocation failure could be caused by broken connection, error in handling the arguments over the wire, etc
- A method only starts executing after invocation is successful
- Invocation of a method doesn't immediately start its execution
See also: Overview of Remote Method Invocation. When you consider the method to be remote, the difference between invocation (a request to start the execution of something) and execution (something that is happening somewhere if the request is successful) becomes more apparent.
Consider also the case with reflection. This is a method of java.lang.reflect.Method
:
public Object invoke(Object obj, Object... args) throws
IllegalAccessException, // failure during invocation
IllegalArgumentException, // failure during invocation
InvocationTargetException // invocation was successful,
// but exception was thrown during execution
Here also clearly invocation and execution are two different things. If you need more convincing, consider the case of an invocation vs execution NullPointerException
in this reflection context:
- It can be thrown during invocation, when
obj == null
when the method is an instance method - It can be thrown during execution, in which case it will be wrapped as the cause of an
InvocationTargetException
I'm not aware of any standard definitions of those, but my understanding is this:
- invocation is the act of calling a method
- execution is the act of actually running the method
Invocation results in execution.