Call an object method

From Rosetta Code
Revision as of 21:45, 4 September 2011 by rosettacode>Spoon! (added ruby)
Call an object method is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

ActionScript

<lang actionscript>// Static MyClass.method(someParameter);

// Instance myInstance.method(someParameter);</lang>

C++

<lang cpp>// Static MyClass::method(someParameter);

// Instance myInstance.method(someParameter);</lang>

E

This example is in need of improvement:

Add runnable example.

A method call in E has the syntax recipient.verb(arg1, arg2, ...), where recipient is an expression, verb is an identifier (or a string literal preceded by ::), and argN are expressions.

<lang e>someObject.someMethod(someParameter)</lang>

In E, there are no distinguished "static methods". Instead, it is idiomatic to place methods on the maker of the object. This is very similar to methods on constructors in JavaScript, or class methods in Objective-C.

J

Note: In some languages "everything is an object". This is not the case in J. In J, non-trivial objects will contain arrays. This is a relatively deep issue, beyond the scope of this page, except that: method invocations are relatively rare, in J.

All method invocations must contain two underline characters in J:

Static:

<lang j>methodName_className_ parameters</lang>

and, given an object instance reference:

<lang j>objectReference=: conew 'className'</lang>

an instance invocation could be:

<lang j>methodName__objectReference parameters</lang>

Note that J also supports infix notation when using methods. In this case, there will be a second parameter list, on the left of the method reference.

<lang j>parameters methodName_className_ parameters</lang> or <lang j>parameters methodName__objectReference parameters</lang>

These variations might be useful when building combining words that need to refer to two different kinds of things. But mostly it's to be consistent with the rest of the language.

Finally, note that static methods can be referred to using the same notation as instance methods -- in this case you must have a reference to the class name:

<lang j>classReference=: <'className' methodName__classReference parameters</lang>

This might be useful when you are working with a variety of classes which share a common structure.

You can also refer to a dynamic method in the same fashion that you use to refer to a instance method -- in this case you must know the object name (which is a number). For example, to refer to a method on object 123

<lang j>methodName_123_ parameters</lang>

This last case can be useful when debugging.

Java

Static methods in Java are usually called by using the dot operator on a class name: <lang java>ClassWithStaticMethod.staticMethodName(argument1, argument2);//for methods with no arguments, use empty parentheses</lang> Instance methods are called by using the dot operator on an instance: <lang java>ClassWithMethod varName = new ClassWithMethod(); varName.methodName(argument1, argument2); //or new ClassWithMethod().methodName(argument1, argument2);</lang>

Instance methods may not be called on references whose value is null (throws a NullPointerException).

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 null 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: Thread.currentThread().sleep(1000);. However, since .sleep() is actually a static method, the compiler replaces it with Thread.sleep(1000); (since Thread.currentThread() has a return type of Thread). This makes it clear that the thread that is put to sleep is not under the user's control. However, written as Thread.currentThread().sleep(1000);, it may lead one to falsely believe it to be possible to sleep another thread by doing someOtherThread.sleep(1000); when in fact such a statement would also sleep the current thread.

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>

PicoLisp

Method invocation is syntactically equivalent to normal function calls. Method names have a trailing '>' by convention. <lang PicoLisp>(foo> MyClass) (foo> MyObject)</lang>

Python

<lang python>class MyClass(object): @classmethod def myClassMethod(self, x): pass @staticmethod def myStaticMethod(x): pass def myMethod(self, x): return 42 + x

myInstance = MyClass()

  1. Instance method

myInstance.myMethod(someParameter)

  1. A method can also be retrieved as an attribute from the class, and then explicitly called on an instance:

MyClass.myMethod(myInstance, someParameter)


  1. Class or static methods (rarely used)

MyClass.myClassMethod(someParameter) MyClass.myStaticMethod(someParameter)

  1. You can also call class or static methods on an instance, which will simply call it on the instance's class

myInstance.myClassMethod(someParameter) myInstance.myStaticMethod(someParameter)</lang>

Ruby

<lang ruby>// Class method MyClass.method(someParameter)

// Instance method myInstance.method(someParameter)

// Calling a method with no parameters myInstance.anotherMethod</lang>

Tcl

<lang tcl>package require Tcl 8.6

  1. "Static" (on class object)

MyClass mthd someParameter

  1. Instance

$myInstance mthd someParameter</lang>