Aqua Phoenix
     >>  Lectures >>  Java 1  
 

Navigator
   
 
       
   

1.7 Methods - Definition

In a functional programming language, a function defines a set of expressions and statements that usually take an input to produce an output.

In Object Oriented Programming (OOP), the term function is replaced by the term method. Effectively they both have the same functionality, however, since functions in OOP define behavior within an object, they have been renamed to avoid confusion (or to create more).

Rules and Observations

  • A method is considered a member of a class.
  • A method defines behavior of an object.
  • A method may take input arguments.
  • A method may produce output (return).
  • A method may change state of its object, i.e. modify variables.
  • The notion of a method changing the state of its object is a major feature of OOP. As opposed to maintaining data structures for a program and using functions to selectively change data, objects in OOP contain data AND functions (methods) to change and manage the data.
  • The general structure of a method is:

    KEYWORDS  RETURN_TYPE  METHOD_NAME  ( ARG1, ARG2, ..., ARGN ) {
      ...
    }
  • A more detailed look at the same structure:

    [ SPECIFIER ]  [ static ]  RETURN_TYPE  METHOD_NAME  ( [ ARG1_TYPE ARG1_NAME [, ARG2_TYPE ARG2_NAME [, ...] ] ] ) {

      [ return RETURN_VAR_NAME; ]
    }
    • SPECIFIER: The specifier can take one of 4 values to control access to members (methods) of a class: private, no specifier (blank), protected, and public. Detailed in the following table are the different access levels.

      Specifier Class Package Subclass World  
      private Yes No No No This method can only be called from within the same class. This is typically used for methods that maintain data or state of an object. It is undesirable to have other classes call a private method to possibly break the consistency of data.
      no specifier (blank) Yes Yes No No This method can additionally be called by other classes in the package. A package is a kind of grouping of Java classes (more on packages later)
      protected Yes Yes Yes No This method can additionally be called by classes that have been derived from this class. Derivations refer to Inheritance (covered later)
      public Yes Yes Yes Yes This method can be accessed by any class from anywhere.

    • static: This initializer is optional. When a method (or a variable) is static, then it can be accessed without an object having been instantiated. This is somewhat similar to saying that a method or variable is constant. The purpose of static methods is to provide a means to create generic functions that do not act on instantiated objects, and serve to compute an output given an input. For example, a method to convert inches to cm may be static, because the conversion does not change depending on an object - it is more or less a constant function.
    • RETURN_TYPE: The return type denotes the data type of the return. This may be a primitive data type, e.g. int, or an object. (more on data types later)
    • METHOD_NAME: The name of a method specifies a not necessarily unique name. An object can have several methods by the same name, but with different input arguments. (more on Overloading later)
    • ARG1_TYPE: Data type of the first argument
    • ARG1_NAME: Name of the first argument. This is used within the method to refer to the passed data.
  • Here are some common examples of methods and their intended use:
  • A method without input, without output (void), accessible by all classes. This is typically used to set a generic state of an object:

    public void reset() {

      ...
      // some code to reset all variables
      ...
    }
  • A method with input, without output, accessible by all classes. This is used to set some object-specific variables. It is said to act on its object:

    public void setLastName(String lastName) {

      ...
      // set internal variable to lastName
      ...
    }
  • A method without input, with output, accessible by all classes. This is used to obtain state from an object:

    public String getFirstName() {

      ...
      // return contents of internal firstName variable
      ...
    }
  • A method that computes something, e.g. a conversion, with input, with output, accessible by all classes, and not requiring an instantiated object:

    public int celsiusToFahrenheit(double celsius) {

      ...
      // return the converted value
      ...
    }