Skip to main content

Method

In Turkish
Metot
Pronunciation
METH-ud
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/method

In short

A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").

What is a method in programming?

A method is defined inside a class, or attached to an object, and is called with a dot: cart.addItem(book), "hello".toUpperCase(). Inside the method, the object it was called on is available as this in JavaScript, Java and C#, or as self in Python and Ruby, so the method can use and update that object's fields.

Instance methods work on one particular object, such as account.deposit(50). Static methods, also called class methods, belong to the class itself and don't need an object, like Math.max(3, 7) or a factory such as User.fromJson(data). Many languages also have getters and setters, methods that look like plain property access.

Methods are how objects expose behavior while hiding their internal details. Callers ask the object to do something, order.cancel(), instead of changing its fields directly; the method can check rules, update related data and keep the object valid. Subclasses can override a method to change what it does, which is the basis of polymorphism.

A common misconception is that methods and functions are completely different things. A method is a function; the difference is only that it is tied to an object or class and receives that object implicitly. In JavaScript, a method taken off its object and called alone can even lose its this, a frequent source of bugs.

Key takeaways

  • A method is a function that belongs to an object or class.
  • It reaches its object through this (or self in Python and Ruby).
  • Instance methods work on one object; static methods belong to the class.
  • Methods let objects expose behavior and protect their internal state.
  • Subclasses can override methods, which enables polymorphism.

Example

Instance and static methods (Python)python
class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):          # instance method: works on one account
        if amount <= 0:
            raise ValueError("Amount must be positive")
        self.balance += amount

    @staticmethod
    def fee_for(amount):                # static method: needs no account
        return round(amount * 0.01, 2)

acct = Account("Ada")
acct.deposit(100)
print(acct.balance, Account.fee_for(100))

Readers ask

What is the difference between a method and a function?

A function stands on its own; a method is a function attached to an object or class and called through it. The method also receives that object, as this or self, so it can work with its data.

What is a static method?

A method that belongs to the class rather than to any one object. It is called on the class, like Math.round(2.5), and cannot use instance fields unless an object is passed in.

What is method overriding?

Defining a method in a subclass with the same name as one in its parent class, so objects of the subclass use the new version. It lets different types respond to the same call in their own way.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings