⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

{{task|Basic language learning}} [[Category:Object oriented]] [[Category:Encyclopedia]]

In [[object-oriented programming]] a method is a function associated with a particular class or object. 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.


// Static

// Instance


Ada is a language based on strict typing. Nevertheless, since Ada 95 (the first major revision of the language), Ada also includes the concepts of a class. Types may be tagged, and for each tagged type T there is an associated type T'Class. If you define a method as "procedure Primitive(Self: T)", the actual parameter Self must be of type T, exactly, and the method Primitive will be called, well, statically. This may be surprising, if you are used to other object-oriented languages.

If you define a method as "prodedure Dynamic(Self: T'Class)", the actual parameter can be either T or any of its descendents. Now, if you call Self.Primitive within the procedure Dynamic, it will be dispatching, i.e., it will call the primitive function matching the type of Self (i.e., either T or any of its subtype). This is what you would expect in many other object-oriented languages.

Finally, a static method can be defined as a subprogram within the same package that holds the object type and the other methods.

Specify the class My_Class, with one primitive subprogram, one dynamic subprogram and a static subprogram:

   package My_Class is
      type Object is tagged private;
      procedure Primitive(Self: Object); -- primitive subprogram
      procedure Dynamic(Self: Object'Class);
      procedure Static;
      type Object is tagged null record;
   end My_Class;

Implement the package:

   package body My_Class is
      procedure Primitive(Self: Object) is
	 Put_Line("Hello World!");
      end Primitive;

      procedure Dynamic(Self: Object'Class) is
	 Put("Hi there! ... ");
	 Self.Primitive; -- dispatching call: calls different subprograms,
                         -- depending on the type of Self
      end Dynamic;

      procedure Static is
      end Static;
   end My_Class;

Specify and implement a subclass of My_Class:

   package Other_Class is
      type Object is new My_Class.Object with null record;
      overriding procedure Primitive(Self: Object);
   end Other_Class;

   package body Other_Class is
      procedure Primitive(Self: Object) is
	 Put_Line("Hello Universe!");
      end Primitive;
   end Other_Class;

The main program, making the dynamic and static calls:

with Ada.Text_IO; use Ada.Text_IO;

procedure Call_Method is

   package My_Class is ... -- see above
   package body My_Class is ... -- see above

   package Other_Class is ... -- see above
   package body Other_Class is ... -- see above

   Ob1: My_Class.Object; -- our "root" type
   Ob2: Other_Class.Object; -- a type derived from the "root" type

end Call_Method;


Hello World!
Hello Universe!
Hi there! ... Hello World!
Hi there! ... Hello Universe!


// Static

// Instance


Person #{
	name ""
	surname ""

	init [n,s]{
		name n
		surname s

	sayHello {
		print "Hello " + name + "!"

person $(new ~Person "John" "Doe")


Hello John!


{{works with|AutoHotkey_L}} (AutoHotkey Basic does not have classes)

class myClass
		MsgBox % SomeParameter

myInstance := new myClass


Bracmat has no classes. Rather, objects can be created as copies of other objects. In the example below, the variable MyObject has a copy of variable MyClass as value. Notice the extra level of reference in (MyObject..Method)$argument, comparable to MyObject->Method(argument) in C. Rather than being the object itself, MyObject points to an object and can be aliased by assigning to another variable.

Inside an object, the object is represented by its, comparable to this in other languages.

( ( myClass
  =   (name=aClass)
      ( Method
      = .out$(str$("Output from " !(its.name) ": " !arg))
& (myClass.Method)$"Example of calling a 'class' method"
& new$(myClass,object1):?MyObject
& (MyObject..Method)$"Example of calling an instance method"
& !MyObject:?Alias
& (Alias..Method)$"Example of calling an instance method from an alias"


Output from aClass: Example of calling a 'class' method
Output from object1: Example of calling an instance method
Output from object1: Example of calling an instance method from an alias


MyClass myClassObject; myClassObject.myFunction(some parameter);

## C++

// Static

// Instance

// Pointer


// Static

// Instance


(Long/toHexString 15) ; use forward slash for static methods

(.equals 1 2)    ; use dot operator to call instance methods
(. 1 (equals 2)) ; alternative style


COBOL has two ways to invoke a method: the INVOKE statement and inline method invocation.

INVOKE FooClass "someMethod" RETURNING bar        *> Factory object
INVOKE foo-instance "anotherMethod" RETURNING bar *> Instance object

*> Inline method invocation
MOVE FooClass::"someMethod" TO bar        *> Factory object
MOVE foo-instance::"anotherMethod" TO bar *> Instance object

To call factory methods of objects of an unknown type (such as when you may have a subclass of the class wanted), it is necessary to get a reference to the class's factory object by calling the "FactoryObject" method.

INVOKE foo-instance "FactoryObject" RETURNING foo-factory
*> foo-factory can be treated like a normal object reference.
INVOKE foo-factory "someMethod"


While CoffeeScript does provide a useful class abstraction around its prototype-based inheritance, there aren't any actual classes.

class Foo
    @staticMethod: -> 'Bar'

    instanceMethod: -> 'Baz'

foo = new Foo

foo.instanceMethod() #=> 'Baz'
Foo.staticMethod() #=> 'Bar'

Common Lisp

In Common Lisp, classmethods are methods that apply to classes, rather than classes that contain methods.

(defclass my-class ()
    :accessor get-x  ;; getter function
    :initarg :x      ;; arg name
    :initform 0)))   ;; initial value

;; declaring a public class method
(defmethod square-x ((class-instance my-class))
  (* (get-x class-instance) (get-x class-instance)))

;; create an instance of my-class
(defvar *instance*
  (make-instance 'my-class :x 10))

(format t "Value of x: ~a~%" (get-x *instance*))

(format t "Value of x^2: ~a~%" (square-x *instance*))

Output (CLISP v2.49):

$ clisp object.cl
Value of x: 10
Value of x^2: 100


struct Cat {
    static int staticMethod() {
        return 2;

    string dynamicMethod() { // Never virtual.
        return "Mew!";

class Dog {
    static int staticMethod() {
        return 5;

    string dynamicMethod() { // Virtual method.
        return "Woof!";

void main() {
    // Static methods calls:
    assert(Cat.staticMethod() == 2);
    assert(Dog.staticMethod() == 5);

    Cat c; // This is a value on the stack.
    Dog d; // This is just a reference, set to null.

    // Other static method calls, discouraged:
    assert(c.staticMethod() == 2);
    assert(d.staticMethod() == 5);

    // Instance method calls:
    assert(c.dynamicMethod() == "Mew!");
    d = new Dog;
    assert(d.dynamicMethod() == "Woof!");


Dyalect supports both instance and static methods. Instance methods have a special this reference that returns an instance. Static method are always invoked through the type name.

//Static method on a built-in type Integer
static func Integer.div(x, y) {
    x / y

//Instance method
func Integer.div(n) {
    this / n

print(Integer.div(12, 3))




{{improve|E|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.


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.


The message call:

    console.printLine("Hello"," ","World!");


Elixir doesn't do objects. Instead of calling methods on object you send messages to processes. Here's an example of a process created with spawn_link which knows how to receive a message "concat" and return a result.

defmodule ObjectCall do
  def new() do
    spawn_link(fn -> loop end)

  defp loop do
    receive do
      {:concat, {caller, [str1, str2]}} ->
        result = str1 <> str2
        send caller, {:ok, result}

  def concat(obj, str1, str2) do
    send obj, {:concat, {self(), [str1, str2]}}

    receive do
      {:ok, result} ->

obj = ObjectCall.new()

IO.puts(obj |> ObjectCall.concat("Hello ", "World!"))


In Factor, there is no distinction between instance and static methods. Methods are contained in generic words and specialize on a class. Generic words define a method combination so methods know which object(s) to dispatch on. (But most methods dispatch on the object at the top of the data stack.) Under this object model, calling a method is no different than calling any other word.

USING: accessors io kernel literals math sequences ;
IN: rosetta-code.call-a-method

! Define some classes.
TUPLE: cat sassiness ;

! Define a constructor for cat.
C: <cat> cat

! Define a generic word that dispatches on the object at the top
! of the data stack.
GENERIC: speak ( obj -- )

! Define methods in speak which specialize on various classes.
M: dog speak drop "Woof!" print ;
M: cat speak sassiness>> 0.5 > "Hiss!" "Meow!" ? print ;
M: object speak drop "I don't know how to speak!" print ;

! Place some objects of various classes in a sequence.
${ dog 0.75 <cat> 0.1 <cat> 10 }
! Call speak, a method, just like any other word.
[ speak ] each


I don't know how to speak!


{{works with|4tH|3.62.0}} {{trans|D}} There are numerous, mutually incompatible object oriented frameworks for Forth. This one works with the FOOS preprocessor extension of [[4tH]].

include lib/compare.4th
include 4pp/lib/foos.4pp

[ASSERT]                               \ enable assertions

:: Cat
     method: dynamicCat                \ virtual method
   end-class {

    :static staticCat { 2 } ;          \ static method
    :method { s" Mew!" } ; defines dynamicCat
  }                                    \ for unrelated classes,
;                                      \ method names have to differ

:: Dog
     method: dynamicDog                \ virtual method
   end-class {

    :static staticDog { 5 } ;
    :method { s" Woof!" } ; defines dynamicDog
  }                                    \ for unrelated classes,
;                                      \ method names have to differ

static Cat c                           \ create two static objects
static Dog d

: main
  assert( class -> staticCat 2 = )     \ check for valid method return
  assert( class -> staticDog 5 = )     \ of a static method

  assert( c -> staticCat 2 = )         \ check for valid method return
  assert( d -> staticDog 5 = )         \ of a static method

  assert( c => dynamicCat s" Mew!"  compare 0= )
  assert( d => dynamicDog s" Woof!" compare 0= )
;                                      \ same for dynamic methods


Works with any ANS Forth

Needs the FMS-SI (single inheritance) library code located here: http://soton.mpeforth.com/flag/fms/index.html

include FMS-SI.f

:class animal
  variable cnt 0 cnt ! \ static instance variable
  :m init: 1 cnt +! ;m
  :m cnt: cnt @ . ;m

:class cat <super animal
  :m speak ." meow" ;m

:class dog <super animal
  :m speak ." woof" ;m

cat Frisky   \ instantiate a cat object named Frisky
dog Sparky   \ instantiate a dog object named Sparky

\ The class method cnt: will return the number of animals instantiated
\ regardless of which animal object is used.

\ The instance method speak will respond differently depending
\ on the class of the instance object.

Frisky cnt:  \ => 2 ok
Sparky cnt:  \ => 2 ok
Frisky speak \ => meow ok
Sparky speak \ => woof ok


In modern Fortran a "derived type" concept corresponds to "class" in OOP. Such types have "type bound procedures", i.e. static methods. Procedure pointer components depend on the value of the object (so they are object-bound), can be redefined runtime and correspond approx to instances.

! type declaration
type my_type
procedure, pass :: method1
procedure, pass, pointer :: method2
end type my_type

! declare object of type my_type
type(my_type) :: mytype_object

!static call
 call mytype_object%method1() ! call method1 defined as subroutine
 mytype_object%method2() ! call method2 defined as function


' FB 1.05.0 Win64

Type MyType
    Declare Sub InstanceMethod(s As String)
    Declare Static Sub StaticMethod(s As String)
    dummy_ As Integer ' types cannot be empty in FB
End Type

Sub MyType.InstanceMethod(s As String)
  Print s
End Sub

Static Sub MyType.StaticMethod(s As String)
  Print s
End Sub

Dim t As MyType
t.InstanceMethod("Hello world!")
MyType.Staticmethod("Hello static world!")
Print "Press any key to quit the program"


Hello world!
Hello static world!


Go distances itself from the word "object" and from many object oriented concepts. It does however have methods. Any user-defined type in Go can have methods and these work very much like "instance methods" of object oriented languages. The examples below illustrate details of Go methods and thus represent the concept of instance methods.

type Foo int // some custom type

// method on the type itself; can be called on that type or its pointer
func (self Foo) ValueMethod(x int) { }

// method on the pointer to the type; can be called on pointers
func (self *Foo) PointerMethod(x int) { }

var myValue Foo
var myPointer *Foo = new(Foo)

// Calling value method on value
// Calling pointer method on pointer

// Value methods can always be called on pointers
// equivalent to (*myPointer).ValueMethod(someParameter)

// In a special case, pointer methods can be called on values that are addressable (i.e. lvalues)
// equivalent to (&myValue).PointerMethod(someParameter)

// You can get the method out of the type as a function, and then explicitly call it on the object
Foo.ValueMethod(myValue, someParameter)
(*Foo).PointerMethod(myPointer, someParameter)
(*Foo).ValueMethod(myPointer, someParameter)

Go has no direct equivalent to class methods as long as you think of a Go type as a class. A Go ''package'' however can be organized and used much like a class, and in this context, any function exported from the package works much like a class method.

An example package:

package box

import "sync/atomic"

var sn uint32

type box struct {
    Contents string
    secret uint32

func New() (b *box) {
    b = &box{secret: atomic.AddUint32(&sn, 1)}
    switch sn {
    case 1:
        b.Contents = "rabbit"
    case 2:
        b.Contents = "rock"

func (b *box) TellSecret() uint32 {
    return b.secret

func Count() uint32 {
    return atomic.LoadUint32(&sn)

Example use:

package main

import "box"

func main() {
    // Call constructor.  Technically it's just an exported function,
    // but it's a Go idiom to naming a function New that serves the purpose
    // of a constructor.
    b := box.New()

    // Call instance method.  In Go terms, simply a method.

    // Call class method.  In Go terms, another exported function.

==Icon and {{header|Unicon}}== Icon does not have objects or methods; they can be implemented on top of other types such as records.

Unicon has no concept of static methods; all methods are normally invoked using an instance of a class. While it is technically possible to call a method without an instance, it requires knowledge of the underlying implementation, such as the name-mangling conventions, and is non-standard.

procedure main()

   bar := foo()  # create instance
   bar.m2()      # call method m2 with self=bar, an implicit first parameter

   foo_m1( , "param1", "param2")  # equivalent of static class method, first (self) parameter is null

class foo(cp1,cp2)
   method m1(m1p1,m1p2)
      local ml1
      static ms1
      ml1 := m1p1
      # do something
   method m2(m2p1)
      # do something else
   L := [cp1]


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:


methodName_className_ parameters

and, given an object instance reference:

objectReference=:'' conew 'className'

an instance invocation could be:

methodName__objectReference parameters

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.

parameters methodName_className_ parameters


parameters methodName__objectReference parameters

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:

classReference=: <'className'
methodName__classReference parameters

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

methodName_123_ parameters

This last case can be useful when debugging.


Static methods in Java are usually called by using the dot operator on a class name:

ClassWithStaticMethod.staticMethodName(argument1, argument2);//for methods with no arguments, use empty parentheses

Instance methods are called by using the dot operator on an instance:

ClassWithMethod varName = new ClassWithMethod();
varName.methodName(argument1, argument2);
new ClassWithMethod().methodName(argument1, argument2);

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.


If you have a object called x and a method called y then you can write:



module Animal

abstract type Animal end
abstract type Dog <: Animal end
abstract type Cat <: Animal end

struct Collie <: Dog

struct Persian <: Cat

# This function is static: it works on any dog, even on a blank instance, in
#   the same way, to produce the same output for anything that isa Dog in type.
function soundalert(a::T) where T <: Dog

# This function depends on the class instance's data for its value, so this type
#   of function is one way to do an instance method in Jula.
masskg(x) = x.weight


Kotlin does not have static methods as such but they can be easily simulated by 'companion object' methods :

class MyClass {
    fun instanceMethod(s: String) = println(s)

    companion object {
        fun staticMethod(s: String) = println(s)

fun main(args: Array<String>) {
    val mc = MyClass()
    mc.instanceMethod("Hello instance world!")
    MyClass.staticMethod("Hello static world!")


Hello instance world!
Hello static world!


In Latitude, everything is an object. Being prototype-oriented, Latitude does not distinguish between classes and instances. Calling a method on either a class or an instance is done by simple juxtaposition.

myObject someMethod (arg1, arg2, arg3).
MyClass someMethod (arg1, arg2, arg3).

The parentheses on the argument list may be omitted if there is at most one argument and the parse is unambiguous.

myObject someMethod "string constant argument".
myObject someMethod (argument). ;; Parentheses are necessary here
myObject someMethod. ;; No arguments

Finally, a colon may be used instead of parentheses, in which case the rest of the line is considered to be the argument list.

myObject someMethod: arg1, arg2, arg3.


LFE/Erlang doesn't have an object system. However, since LFE is a Lisp, one can do the same thing in LFE as one did in Lisps before CLOS: use closures to create and work with objects.

Not only that, but one may also use Erlang lightweight process to accomplish something very similar!

Examples for both approaches are given below.

With Closures

(defmodule aquarium
 (export all))

(defun fish-class (species)
  This is the constructor that will be used most often, only requiring that
  one pass a 'species' string.

  When the children are not defined, simply use an empty list.
  (fish-class species ()))

(defun fish-class (species children)
  This contructor is mostly useful as a way of abstracting out the id
  generation from the larger constructor. Nothing else uses fish-class/2
  besides fish-class/1, so it's not strictly necessary.

  When the id isn't know, generate one.
  (let* (((binary (id (size 128))) (: crypto rand_bytes 16))
         (formatted-id (car
                         (: io_lib format
                           '"~32.16.0b" (list id)))))
    (fish-class species children formatted-id)))

(defun fish-class (species children id)
  This is the constructor used internally, once the children and fish id are
  (let ((move-verb '"swam"))
    (lambda (method-name)
      (case method-name
          (lambda (self) id))
          (lambda (self) species))
          (lambda (self) children))
          (lambda (self)
            (: io format
              '"id: ~p~nspecies: ~p~nchildren: ~p~n"
              (list (get-id self)
                    (get-species self)
                    (get-children self)))))
          (lambda (self distance)
            (: io format
              '"The ~s ~s ~p feet!~n"
              (list species move-verb distance))))
          (lambda (self)
            (let* ((child (fish-class species))
                   (child-id (get-id child))
                   (children-ids (: lists append
                                   (list children (list child-id))))
                   (parent-id (get-id self))
                   (parent (fish-class species children-ids parent-id)))
              (list parent child))))
          (lambda (self)
            (: erlang length children)))))))

(defun get-method (object method-name)
  This is a generic function, used to call into the given object (class
  (funcall object method-name))

; define object methods
(defun get-id (object)
  (funcall (get-method object 'id) object))

(defun get-species (object)
  (funcall (get-method object 'species) object))

(defun get-info (object)
  (funcall (get-method object 'info) object))

(defun move (object distance)
  (funcall (get-method object 'move) object distance))

(defun reproduce (object)
  (funcall (get-method object 'reproduce) object))

(defun get-children (object)
  (funcall (get-method object 'children) object))

(defun get-children-count (object)
  (funcall (get-method object 'children-count) object))

With this done, one can create objects and interact with them. Here is some usage from the LFE REPL:

; Load the file and create a fish-class instance:

> (slurp '"object.lfe")
#(ok object)
> (set mommy-fish (fish-class '"Carp"))

; Execute some of the basic methods:

> (get-species mommy-fish)
> (move mommy-fish 17)
The Carp swam 17 feet!
> (get-id mommy-fish)

; Now let's look at "modifying" state data (e.g., children counts):

> (get-children mommy-fish)
> (get-children-count mommy-fish)
> (set (mommy-fish baby-fish-1) (reproduce mommy-fish))
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
> (get-id mommy-fish)
> (get-id baby-fish-1)
> (get-children-count mommy-fish)
> (set (mommy-fish baby-fish-2) (reproduce mommy-fish))
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
> (get-id mommy-fish)
> (get-id baby-fish-2)
> (get-children-count mommy-fish)
> (get-info mommy-fish)
id: "47eebe91a648f042fc3fb278df663de5"
species: "Carp"
children: ["fdcf35983bb496650e558a82e34c9935",

With Lightweight Processes

(defmodule object
 (export all))

(defun fish-class (species)
  This is the constructor that will be used most often, only requiring that
  one pass a 'species' string.

  When the children are not defined, simply use an empty list.
  (fish-class species ()))

(defun fish-class (species children)
  This constructor is useful for two reasons:
    1) as a way of abstracting out the id generation from the
       larger constructor, and
    2) spawning the 'object loop' code (fish-class/3).
  (let* (((binary (id (size 128))) (: crypto rand_bytes 16))
         (formatted-id (car
                         (: io_lib format
                           '"~32.16.0b" (list id)))))
    (spawn 'object
           (list species children formatted-id))))

(defun fish-class (species children id)
  This function is intended to be spawned as a separate process which is
  used to track the state of a fish. In particular, fish-class/2 spawns
  this function (which acts as a loop, pattern matching for messages).
  (let ((move-verb '"swam"))
      ((tuple caller 'move distance)
        (! caller (list species move-verb distance))
        (fish-class species children id))
      ((tuple caller 'species)
        (! caller species)
        (fish-class species children id))
      ((tuple caller 'children)
        (! caller children)
        (fish-class species children id))
      ((tuple caller 'children-count)
        (! caller (length children))
        (fish-class species children id))
      ((tuple caller 'id)
        (! caller id)
        (fish-class species children id))
      ((tuple caller 'info)
        (! caller (list id species children))
        (fish-class species children id))
      ((tuple caller 'reproduce)
        (let* ((child (fish-class species))
               (child-id (get-id child))
               (children-ids (: lists append
                               (list children (list child-id)))))
        (! caller child)
        (fish-class species children-ids id))))))

(defun call-method (object method-name)
  This is a generic function, used to call into the given object (class
  (! object (tuple (self) method-name))
    (data data)))

(defun call-method (object method-name arg)
  Same as above, but with an additional argument.
  (! object (tuple (self) method-name arg))
    (data data)))

; define object methods
(defun get-id (object)
  (call-method object 'id))

(defun get-species (object)
  (call-method object 'species))

(defun get-info (object)
  (let ((data (call-method object 'info)))
    (: io format '"id: ~s~nspecies: ~s~nchildren: ~p~n" data)))

(defun move (object distance)
  (let ((data (call-method object 'move distance)))
    (: io format '"The ~s ~s ~p feet!~n" data)))

(defun reproduce (object)
  (call-method object 'reproduce))

(defun get-children (object)
  (call-method object 'children))

(defun get-children-count (object)
  (call-method object 'children-count))

Here is some usage from the LFE REPL:

; Load the file and create a fish-class instance:

> (slurp '"object.lfe")
#(ok object)
> (set mommy-fish (fish-class '"Carp"))

; Execute some of the basic methods:

> (get-species mommy-fish)
> (move mommy-fish 17)
The Carp swam 17 feet!
> (get-id mommy-fish)

; Now let's look at modifying state data:

> (get-children mommy-fish)
> (get-children-count mommy-fish)
> (set baby-fish-1 (reproduce mommy-fish))
> (get-id mommy-fish)
> (get-id baby-fish-1)
> (get-children-count mommy-fish)
> (set baby-fish-2 (reproduce mommy-fish))
> (get-id mommy-fish)
> (get-id baby-fish-2)
> (get-children-count mommy-fish)
> (get-info mommy-fish)
id: 47eebe91a648f042fc3fb278df663de5
species: Carp
children: ["fdcf35983bb496650e558a82e34c9935",


-- call static method

-- call instance method
obj = script("MyClass").new()


In Logtalk, class or instance are ''roles'' that an object can play depending on the relations in other objects. Thus, a "class" can be defined by having an object specializing another object and an instance can be defined by having an object instantiating another object. Metaclasses are easily defined and thus a class method is just an instance method defined in the class metaclass.

% avoid infinite metaclass regression by
% making the metaclass an instance of itself
:- object(metaclass,

    :- public(me/1).
    me(Me) :-

:- end_object.

:- object(class,

    :- public(my_class/1).
    my_class(Class) :-
        instantiates_class(Self, Class).

:- end_object.

:- object(instance,

:- end_object.


| ?- class::me(Me).
Me = class

| ?- instance::my_class(Class).
Class = class


Lua does not provide a specific OO implementation, but its metatable mechanism and sugar for method-like calls does make it easy to implement and use such as system, as evidenced by its multitude of class libraries in particular.

Instance methods, in their most basic form, are simply function calls where the calling object reference is duplicated and passed as the first argument. Lua offers some syntactical sugar to facilitate this, via the colon operator:

local object = { name = "foo", func = function (self) print(self.name) end }

object:func() -- with : sugar
object.func(object) -- without : sugar

Using metatables, and specifically the __index metamethod, it is possible to call a method stored in an entirely different table, while still passing the object used for the actual call:

local methods = { }
function methods:func () -- if a function is declared using :, it is given an implicit 'self' parameter

local object = setmetatable({ name = "foo" }, { __index = methods })

object:func() -- with : sugar
methods.func(object) -- without : sugar

Lua does not have a specific way of handling static methods, but similarly to Go, they could simply be implemented as regular functions inside of a module.

Example module, named box.lua:

local count = 0
local box = { }
local boxmt = { __index = box }
function box:tellSecret ()
    return self.secret

local M = { }
function M.new ()
    count = count + 1
    return setmetatable({ secret = count, contents = count % 2 == 0 and "rabbit" or "rock" }, boxmt)
function M.count ()
    return count
return M

Example use:

local box = require 'box'

local b = box.new()


M2000 Interpreter

In M2000 there are some kinds of objects. Group is the one type object. We can compose members to groups, with functions, operators, modules, events, and inner groups, among other members of type pointer to object. Another type is the COM type. Here we see the Group object

Module CheckIt {
      \\ A class definition is a function which return a Group
      \\ We can make groups and we can alter them using Group statement
      \\ Groups may have other groups inside

      Group Alfa {
            Group SetValue {
                  Set (x) {
                        Link parent myvalue to m
            Module MyMethod {
                 Read x
                 Print x*.myvalue

      Alfa.MyMethod 5 '500
      Alfa.MyMethod %x=200   ' 20000
      \\ we can copy Alfa to Z
      Z.MyMethod 5
      Z.MyMethod 5 ' 1500
      Alfa.MyMethod 5    ' 500
      Dim A(10)
      A(3).MyMethod 5 '1500
      A(3).MyMethod 5 '1000
      \\ get a pointer of group in A(3)
      A(3).MyMethod 5 '500
      \\ k get pointer to Alfa
      Alfa.MyMethod 5 '2500
      k=>MyMethod 5 ' 1500
      k=>MyMethod 5 ' 500


There is no real difference in how you call a static or instance method.

# Static
Method( obj, other, arg );
# Instance
Method( obj, other, arg );


MiniScript uses prototype-based inheritance, so the only difference between a static method and an instance method is in whether it uses the self pseudo-keyword.

Dog = {}
Dog.name = ""
Dog.help = function()
    print "This class represents dogs."
end function
Dog.speak = function()
    print self.name + " says Woof!"
end function

fido = new Dog
fido.name = "Fido"

Dog.help    // calling a "class method"
fido.speak  // calling an "instance method"


This class represents dogs.
Fido says Woof!


// Static

// Instance


Like [[#Java|Java]], static methods in NetRexx are called by using the dot operator on a class name:


Instance methods are called by using the dot operator on an instance:

objectInstance = SomeClass() -- create a new instance of the class
objectInstance.instanceMethod() -- call the instance method

SomeClass().instanceMethod() -- same as above; create a new instance of the class and call the instance method immediately


In Nim there are no object methods, but regular procedures can be called with method call syntax:

var x = @[1, 2, 3]
add(x, 4)

OASYS Assembler

The OASYS runtime is strange; all classes share the same methods and properties, there are no static methods and properties, and there are no procedures/functions other than methods on objects. However, the initialization method can be called on a null object (no other method has this possibility).

The following code calls a method called &GO on the current object: +&GO

## Objeck


ClassName->some_function(); # call class function
instance->some_method(); # call instance method

Objeck uses the same syntax for instance and class method calls. In Objeck, functions are the equivalent of public static methods.

Object Pascal

Object Pascal as implemented in Delphi and Free Pascal supports both static (known in Pascal as class method) and instance methods. Free Pascal has two levels of static methods, one using the class keyword and one using both the class and the static keywords. A class method can be called in Pascal, while a static class method could be called even from C, that's the main difference.

// Static (known in Pascal as class method)

// Instance

=={{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 (see also: Smalltalk).

// Class
[MyClass method:someParameter];
// or equivalently:
id foo = [MyClass class];
[foo method:someParameter];

// Instance
[myInstance method:someParameter];

// Method with multiple arguments
[myInstance methodWithRed:arg1 green:arg2 blue:arg3];

// Method with no arguments
[myInstance method];


We can only call the method of an instantiated object:

my_obj#my_meth params


When a method is called, the top of the stack is used as the object on which the method will be applyed :

For class methods, the top of the stack must be a class (which is also an object of Class class) :

```Oforth>Date now</lang

## ooRexx

say "pi:" .circle~pi
say "c~area:" c~area
Do r=2 To 10
say .circle~instances('') 'circles were created'

::class circle
::method pi class -- a class method
  return 3.14159265358979323

::method instances class -- another class method
  expose in
  use arg a
  If datatype(in)<>'NUM' Then in=0
  If a<>'' Then
  Return in

::method init
  expose radius
  use arg radius

::method area     -- an instance method
  expose radius
  Say self~class
  Say self
  return self~class~pi * radius * radius


pi: 3.14159265358979323
The CIRCLE class
c~area: 3.14159265
10 circles were created


# Class method
# Equivalently using a class name
my $foo = 'MyClass';

# Instance method

# Calling a method with no parameters

# Class and instance method calls are made behind the scenes by getting the function from
# the package and calling it on the class name or object reference explicitly
MyClass::classMethod('MyClass', $someParameter);
MyClass::method($myInstance, $someParameter);

Perl 6

{{works with|Rakudo|2015.12}}

Basic method calls

class Thing {
  method regular-example() { say 'I haz a method' }

  multi method multi-example() { say 'No arguments given' }
  multi method multi-example(Str $foo) { say 'String given' }
  multi method multi-example(Int $foo) { say 'Integer given' }

# 'new' is actually a method, not a special keyword:
my $thing = Thing.new;

# No arguments: parentheses are optional

# Arguments: parentheses or colon required
$thing.multi-example("This is a string");
$thing.multi-example: "This is a string";
$thing.multi-example: 42;

# Indirect (reverse order) method call syntax: colon required
my $foo = new Thing: ;
multi-example $thing: 42;

=== Meta-operators ===

The . operator can be decorated with meta-operators.

my @array = <a z c d y>;
@array .= sort;  # short for @array = @array.sort;

say @array».uc;  # uppercase all the strings: A C D Y Z

Classless methods

A method that is not in a class can be called by using the & sigil explicitly.

my $object = "a string";  # Everything is an object.
my method example-method {
    return "This is { self }.";

say $object.&example-method;  # Outputs "This is a string."


// Static method
// In PHP 5.3+, static method can be called on a string of the class name
$foo = 'MyClass';

// Instance method


Method invocation is syntactically equivalent to normal function calls. Method names have a trailing '>' by convention.

(foo> MyClass)
(foo> MyObject)


Pike does not have static methods. (the static modifier in Pike is similar to protected in C++).

regular methods can be called in these ways:


call_function() is rarely used anymore.

because () is actually an operator that is applied to a function reference, the following is also possible:

function func = obj->method;

as alternative to static function, modules are used. a module is essentially a static class. a function in a module can be called like this:


it should be noted that module.func is resolved at compile time while module["func"] is resolved at runtime.


create or replace TYPE myClass AS OBJECT (
    -- A class needs at least one member even though we don't use it
    dummy NUMBER,
        RETURN 'Called myClass.static_method';
    END static_method;

        RETURN 'Called myClass.instance_method';
    END instance_method;

    myInstance myClass;
    myInstance := myClass(null);
    DBMS_OUTPUT.put_line( myClass.static_method() );
    DBMS_OUTPUT.put_line( myInstance.instance_method() );


$Date = Get-Date
$Date.AddDays( 1 )
[System.Math]::Sqrt( 2 )


// define a rudimentary class
class HelloWorld
    public static void sayHello()
        println("Hello, world!");
    public void sayGoodbye()
        println("Goodbye, cruel world!");

// call the class method

// create an instance of the class
HelloWorld hello = new HelloWorld();

// and call the instance method


class MyClass(object):
	def myClassMethod(self, x):
	def myStaticMethod(x):
	def myMethod(self, x):
		return 42 + x

myInstance = MyClass()

# Instance method
# A method can also be retrieved as an attribute from the class, and then explicitly called on an instance:
MyClass.myMethod(myInstance, someParameter)

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


The following snippet shows a call to the start method of the timer% class.

#lang racket/gui

(define timer (new timer%))
(send timer start 100)


new point { print() }
Class Point
        x = 10  y = 20  z = 30
        func print see x + nl + y + nl + z + nl

o1 = new System.output.console
o1.print("Hello World")

Package System.Output
        Class Console
                Func Print cText
                        see cText + nl


# Class method

# Class may be computed at runtime
foo = MyClass

# Instance method

# The parentheses are optional
my_instance.method some_parameter

# Calling a method with no parameters


struct Foo;

impl Foo {
    // implementation of an instance method for struct Foo
    // returning the answer to life
    fn get_the_answer_to_life(&self) -> i32 {

    // implementation of a static method for struct Foo
    // returning a new instance object
    fn new() -> Foo {
        println!("Hello, world!");
        Foo // returning the new Foo object

fn main() {
    // create the instance object foo,
    // by calling the static method new of struct Foo
    let foo = Foo::new();

    // get the answer to life
    // by calling the instance method of object foo
    println!("The answer to life is {}.", foo.get_the_answer_to_life());

    // Note that in Rust, methods still work on references to the object.
    // Rust will automatically do the appropriate dereferencing to get the method to work:
    let lots_of_references = &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&foo;
    println!("The answer to life is still {}." lots_of_references.get_the_answer_to_life());


/* This class implicitly includes a constructor which accepts an Int and
 *  creates "val variable1: Int" with that value.
class MyClass(val memberVal: Int) { // Acts like a getter, getter automatically generated.
  var variable2 = "asdf" // Another instance variable; a public mutable this time
  def this() = this(0) // An auxilliary constructor that instantiates with a default value

object HelloObject {
  val s = "Hello" // Not private, so getter auto-generated

/** Demonstrate use of our example class.
object Call_an_object_method extends App {
  val s = "Hello"
  val m = new MyClass
  val n = new MyClass(3)

  assert(HelloObject.s == "Hello") // "Hello" by object getterHelloObject
  assert(m.memberVal == 0)
  assert(n.memberVal == 3)
  println("Successfully completed without error.")


class MyClass {
    method foo(arg) { say arg }

var arg = 42;

# Call a class method

# Alternatively, using an expression for the method name

# Create an instance
var instance = MyClass();

# Instance method

# Alternatively, by using an expression for the method name

# Alternatively, by asking for a method


In Smalltalk, 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 strictly speaking, there are no "static" methods. Often in literature, Smalltalk's class messages are described as synonymous to static function calls. This is wrong, because class methods can be overwritten in subclasses just as any other message. Classes are first class objects, meaning that references to them can be passed as argument, stored in other objects or returned as the result of a message send. This makes some of the common design patterns which deal with creation of objects trivial or even superfluous. (see also: ObjectiveC)

" Class "
MyClass selector: someArgument .
" or equivalently "
foo := MyClass .
foo selector: someArgument.

" Instance "
myInstance selector: someArgument.

" Message with multiple arguments "
myInstance fooWithRed:arg1 green:arg2 blue:arg3 .

" Message with no arguments "
myInstance selector.

" Binary (operator) message"
myInstance + argument .

Example for dynamic class determination:

theCar := (someCondition ifTrue:[ Ford ] ifFalse: [ Jaguar ]) new.

Message names (selectors) can be chosen or constructed dynamically at runtime. For example:

whichMessage := #( #'red' #'green' #'blue') at: computedIndex.
foo perform: whichMessage


theMessage := ('handleFileType' , suffix) asSymbol.
foo perform: theMessage.

This is often sometimes used as a dispatch mechanism (also, to implement state machines, for example).

Of course, especially with all that dynamics, a selector for an unimplemented message might be encountered. This raises a MessageNotUnderstood exception (runtime exception).

    foo perform: theMessage
] on: MessageNotUnderstood do:[
   Dialog information: 'sorry'


In SuperCollider, classes are objects. To call a class or object method, a message is passed to the class or the object instance. In the implementation, class method names are prefixed with an asterix, all other methods are instance methods.

	SomeClass {

		*someClassMethod {


		someInstanceMethod {




	a = SomeClass.new;


In Swift, instance methods can be declared on structs, enums, and classes. "Type methods", which include static methods for structs and enums, and class methods for classes, can be called on the type. Class methods are inherited through inheritance of classes, and are resolved dynamically at runtime like instance methods. (see also: Smalltalk).

// Class
// or equivalently:
let foo = MyClass.self

// Instance

// Method with multiple arguments
myInstance.method(red:arg1, green:arg2, blue:arg3)


package require Tcl 8.6
# "Static" (on class object)
MyClass mthd someParameter

# Instance
$myInstance mthd someParameter


# create an instance of the built-in file class
decl file f

# call the file.open method
f.open "filename.txt"


First we have to create a class module named "myObject" :

Option Explicit

Sub Method_1(Optional myStr As String)
Dim strTemp As String
    If myStr <> "" Then strTemp = myStr
    Debug.Print strTemp
End Sub

Static Sub Method_2(Optional myStr As String)
Dim strTemp As String
    If myStr <> "" Then strTemp = myStr
    Debug.Print strTemp
End Sub

In a "standard" Module, the call should be :

Option Explicit

Sub test()
Dim Obj As New myObject
    Obj.Method_1 "Hello to you"
    Obj.Method_2 "What is your name ?"
End Sub


Hello to you
What is your name ?

What is your name ?


Class methods and instance methods are defined using DEFINE-CLASS-METHOD and DEFINE-METHOD respectively. They are called by sending a message to the class or to an instance of it: the message consists of (a) the name of the object that will receive it, which may be a class; (b) the name of the method, as a quoted symbol; and (c) the parameters if any.


    (DISPLAY "I am the class -- ")
    (DISPLAY "You sent me the parameter ")

    (DISPLAY "I am an instance of the class --  ")
    (DISPLAY "You sent me the parameter ")





I am the class -- #<Class:MY-CLASS #x38994c8>
You sent me the parameter FOO
I am an instance of the class --  #<Object:MY-CLASS #x39979d0>
You sent me the parameter BAR


In zkl, a class can be static (there will only exist one instance of the class). A function is always a member of some class but will only be static if it does not refer to instance data.

class C{var v; fcn f{v}}
C.f() // call function f in class C
C.v=5; c2:=C();    // create new instance of C
println(C.f()," ",c2.f()) //-->5 Void
C.f.isStatic //--> False

class [static] D{var v=123; fcn f{v}}
D.f(); D().f(); // both return 123
D.f.isStatic //-->False

class E{var v; fcn f{}} E.f.isStatic //-->True

{{omit from|AWK|not OO}} {{omit from|BASIC|not OO}} {{omit from|bc}} {{omit from|C|not OO}} {{omit from|dc}} {{omit from|GUISS}} {{omit from|Locomotive Basic}} {{omit from|M4}} {{omit from|Mathematica|Cannot create basic classes}} {{omit from|Maxima}} {{omit from|Metafont}} {{omit from|Octave}} {{omit from|PARI/GP}} {{omit from|Retro}} {{omit from|REXX|not OO}} {{omit from|TI-83 BASIC|not OO}} {{omit from|TI-89 BASIC|Does not have user-defined types/classes}} {{omit from|UNIX Shell}} {{omit from|Vim Script}} {{omit from|ZX Spectrum Basic|not OO}}