Method

A method is a subroutine attached to a specific class defined in the source code of a program. It is similar to a function, but can only be called by an object created from a class.

In the Java example below, the method getArea is defined within the class rectangle. In order for the getArea method to be used by a program, an object must first be created from the rectangle class.

class Rectangle
{
   int getArea(int width, int height)
   {
      int area = width * height;
      return area;
   }
}

Methods are an important part of object-oriented programming since they isolate functions to individual objects. The methods within a class can only be called by objects created from the class. Additionally, methods can only reference data known to the corresponding object. This helps isolate objects from each other and prevents methods within one object from affecting other objects.

While methods are designed to isolate data, they can still be used to return values to other classes if necessary. If a value needs to be shared with another class, the return statement (as seen in the example above) can be used.

Updated April 19, 2011 by Per C.

quizTest Your Knowledge

What graphics API is developed by Microsoft?

A
OpenGL
0%
B
Direct3D
0%
C
Metal
0%
D
Glide
0%
Correct! Incorrect!     View the Direct3D definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Method on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation bar directly below the definition.

Our goal to explain computer terminology in a way that is easy to understand. We strive for accuracy and simplicity with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

TechTerms Newsletter

Improve your technical knowledge with our daily or weekly newsletter! Subscribe today and get new terms and quizzes delivered to your inbox.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.