Methods


#1

hello there.

Somebody please explain to me, plain English, what is a method? Don’t be technical!


#2

A method is a function that is encapsulated in a class. It cannot be called directly from anywhere, but must be invoked on either the class itself, or on an instance of the class.

Bottom line, it is a function.

Here is a typical JavaScript example (for simplicity)

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.setAge = function (newAge) {
        this.age = newAge;
    };
}

Above, setAge is a method of the Person class.

var bob = new Person("Bob", 39);

We invoke the method on the instance, bob,

bob.setAge(49);

console.log(bob.age);    // 49

In Java, all functions are written inside a class so essentially all Java functions are methods.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.