Java – Methods

Method:

Method definitions are divided into two parts: a heading and a method body:

public void myMethod()
{
 code to perform action or compute value
}

Methods are invoked using the name of the calling object and the method name as follows:

  • classVar.myMethod();

Invoking a method is equivalent to executing the method body

A method that returns a value must specify the type of that value in its heading:

  • public typeReturned methodName(paramList)

A void method uses the keyword void in its heading to show that it does not return a value

  • public void methodName(paramList)

An invocation of a void method is simply a statement:

  • objectName.methodName();

Testing Methods

  • Driver Program:  A program whose only purpose is to test a method

Accessor methods allow the programmer to obtain the value of an object’s instance variables

  • The data can be accessed but not changed
  • The name of an accessor method typically starts with the word get

Mutator methods allow the programmer to change the value of an object’s instance variables in a controlled manner

  • Incoming data is typically tested and/or filtered
  • The name of a mutator method typically starts with the word set

Calling a method

 // method is called into action by writing the name of the calling object, followed by a dot, followed by the method name, followed by parentheses

  String greeting = "Hello";
  int count = greeting.lenght();
  System.out.println(greeting.length());
   //greeting.length -> calling object.method name (calling object)

  String greeting = "Hello";
  greeting.equalsIgnoreCase("hello") // returns true

Accessors and Mutators

Accessors (Getters) — returns some property of an object

  • accessors are usually named getPropertyName
  • One exception is that accessors that return a boolean value are commonly named isPropertyName

Mutators (Setters) — changes some property of an object

  • mutators are usually named setPropertyName
// Ex 1
public class Employee {
    private String name;
    private final String ssn;
    …
    public void setName(String name) {
        if (name != null && name.length() > 0) {
            this.name = name;
        }
    }

    public String getName() {
        return this.name;
    }

    public String getSsn() {
        return this.ssn;
    }
    …
}
Now, to set the name on an employee in EmployeeDemo.main() (i.e., from the outside), you must call:

e2.setName("Tom");
as opposed to:

e2.name = "Tom"; // won't compile, name is hidden externally
- See more at: http://www.developer.am/java/?page=accessors-mutators#sthash.r1JVc7bW.dpuf

// Ex 2
1 class Cat {
2     private static String animalType = "feline";
3     private String catColor;
4     Cat(String colorIn)
5     {
6         setCatsColor(colorIn);    
7     }
8     public String getCatsColor()
9     {
10        return this.catColor;  
11    }
12    public String setCatsColor()
13    {
14        this.catColor;  
15    }
16    
17    public static void main (String[] argsIn) {
18        Cat patches = new Cat("calico");
19    }
20 } 

Overloading Methods

  • Methods can be overloaded by utilizing the same method name and adding different parameter types for each overloaded method