开发者

Java - Method Invocation & Execution

开发者 https://www.devze.com 2022-12-24 16:44 出处:网络
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:

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.


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.


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
  • 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

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


As far as my knowledge is concern:

Invocation is the pre-step for execution. If invocation is successful then the process of execution starts...

For example,

Parameters (the variables declared in the method signature) will be created only during method invocation.It is the pre-step for execution. After the invocation, the actual method will be executed i.e., the local variables(the variables which are declared in the method body) will be created during the method execution.

so parameters are at invocating and local variables are at executing...

Thus, The successful invocation leads to proceed to execution.

0

精彩评论

暂无评论...
验证码 换一张
取 消