Call an object method: Difference between revisions

added objective-c and some java explanation
(J: minor note of caution)
(added objective-c and some java explanation)
Line 5:
In [[object-oriented programming]] a method is a function associated with a particular class. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
 
Show how to call a static or class method, and an instance method of a class.
 
=={{header|ActionScript}}==
<lang actionscript>// Static
MyClass.method(someParamatersomeParameter);
 
// Instance
myInstance.method(someParamatersomeParameter);</lang>
 
=={{header|C++}}==
<lang cpp>// Static
MyClass::method(someParamatersomeParameter);
 
// Instance
myInstance.method(someParamatersomeParameter);</lang>
 
=={{header|E}}==
Line 75:
varName.methodName(argument1, argument2);
//or
(new ClassWithMethod()).methodName(argument1, argument2);</lang>
 
StaticInstance methods can also be called using instances, but compilers usually complain about that (with a warning, not a failed compilation). The call looks identical to an instance method call. Methods may not be called on uninitialized objects or objects initialized to <code>null</code> (throws a <code>NullPointerException</code>). <!--Maybe add reflection stuff here too? It might be too complicated or it might not belong here-->
 
Note: Static methods can also be called using the dot syntax on a reference to an instance, looking identical to an instance method call, but this is highly discouraged, and compilers usually complain about that (with a warning, not a failed compilation). The reason for this is that such a call is actually equivalent to a static method call on the ''static (compile-time) type'' of the reference in the source code. That means the runtime value of that reference is actually irrelevant, and the reference may be <code>null</code> or be a reference to an instance of a subclass, the compiler will still make it a call to the static method based on the class of the value at compile-time (i.e. inheritance does not apply; the method call is not resolved at runtime).
 
This means that there is no benefit to making a static method call this way, because one can make the call based on the static type that is already known from looking at the code when it is written. Furthermore, such a call may lead to serious pitfalls, leading one to believe that changing the object that it is called on could change the behavior. For example, a common snippet to sleep the thread for 1 second is the following: <code>Thread.currentThread().sleep(1000);</code>. However, since <code>.sleep()</code> is actually a static method, the compiler replaces it with <code>Thread.sleep(1000);</code> (since <code>Thread.currentThread()</code> has a return type of <code>Thread</code>). This makes it clear that the thread that is put to sleep is not under the user's control. However, written as <code>Thread.currentThread().sleep(1000);</code>, it may lead one to falsely believe it to be possible to sleep another thread by doing <code>someOtherThread.sleep(1000);</code> when in fact such a statement would also sleep the current thread.
 
=={{header|Objective-C}}==
In Objective-C, calling an instance method is sending a message to an instance, and calling a class method is sending a message to a class object. Class methods are inherited through inheritance of classes. All messages (whether sent to a normal object or class object) are resolved dynamically at runtime, hence there are no "static" methods.
<lang objc>// Class
[MyClass method:someParameter];
// or equivalently:
id foo = [MyClass class];
[foo method:someParameter];
 
// Instance
[myInstance method:someParameter];</lang>
 
=={{header|PicoLisp}}==
Line 93 ⟶ 109:
>>> MyClass.myMethod(myInstance)
42
>>> # Call instance method implicitelyimplicitly on its instance
>>> myInstance.myMethod()
42
Anonymous user