Methods
Home Up Boolean Operators Classes Conditionals Iteration Methods

 

A method is a procedure (including a special case procedure called a constructor) or a function that is associated with a class.  A procedure doesn't return anything (its return type is void), while a function must return something.

Example Methods

class Easy
{

  Easy ()                 // constructor
  {
    name = "";
    age = 0;
  }

  Easy (String n, int a)   // constructor
  {
    name = n;
    age = a;
  }

  private void printValue ()      // procedure
  {
     System.out.println("The age in one year is " + addOne(age)); 
  }

  public int addOne (int n)    // function
  {
      return (n + 1);    
  }

  private String name;
  private int age;
}

Notice the general template for a method that is not a constructor:  The first line consists of access modifiers, followed by the return type of the method, followed by the name of the method, followed by a list of argument types and names.  The body of the method is then enclosed between a "{" and a "}".

Constructors

Every class should be provided with one or more constructors.  In the example above, there are two constructors.  Typically, the purpose of a constructor is to provide initial values to the data fields associated with an instance of the class.   By convention, the constructor(s) should be the first thing that appears in a class.  Notice the general template for a constructor:  The first line consists of the name of the class, followed by a list of argument types and names.  (By default, constructors are procedures without an access modifier... see below.)  The body of the constructor then appears between the opening and closing braces.

Overloading

Any method (procedures, functions, constructors) can be overloaded.  In the example above, only the constructor is overloaded.  To be overloaded, two or more methods must appear with the same method name, but with different argument lists. 

Access Modifiers

  1. public ... This means that the method can be called from anywhere.
  2. protected ... This means that the method can be called from anywhere within the class where it is defined or any of its subclasses.
  3. nothing specified ... This means that the method can be called from anywhere within the file where the method appears.  All constructors must have this access mode.
  4. private ... This means that the method can only be called from within the class where it is defined.

In addition to one of the above, a method can also be marked as "final".   A final method is one that can not be overridden in a subclass.

Calling a Method

Notice in the Easy example above, that the printValue() procedure calls the addOne() function.  Since these methods are contained within the same class, the call is made in a typical manner.

Life gets more interesting if one wants to call methods from outside the class.   The four examples below are assumed to exist within some other class.  The first two examples show how to call the constructor.  The Java keyword "new" invokes the constructor.  The third example calls the printValue() procedure.  The procedure call must be prefixed with an instance of the class so that the appropriate data field values can be used.  The fourth example calls the addOne() function.  Again, the function call must be prefixed with an instance of the class so that the Java environment can determine which data field values to use.

  1. Easy exampleOne = new Easy ();
  2. Easy exampleTwo = new Easy ("Bill", 39);
  3. exampleOne.printValue();
  4. someVariable = exampleTwo.addOne();