Method

Define a Method

In Python, you define function to create a block of re-usable code. In Java, this is done using method. You must define your method within a Java class. There can be more than one method in each Java class.

public class MyClass {
	public void show() {
		System.out.println("this is a method");
	}
 
	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
 
	public double addNumber(double x, double y) {
		double sum = x + y;
		return sum;
	}
 
}

In the example given above, there are three methods defined in the class MyClass, namely showsayHello andaddNumber.

Syntax of Method Definition

Let's zoom in to each method to understand the syntax of a Java method definition, all method definition will have the syntax as followed:

 
modifier returnValueDataType methodName(list of parameters) {
     method body
}
  • modifier -- it defines who can use/call the method. There are usually two options,
    • private -- only method within the same class can use/call the method
    • public -- anyone can use/call the method. If you are not sure which one to use, just use public
  • returnValueDataType -- it defines the data type of the return value. The return value is usually the result of the work done by the method.
  • methodName -- it defines the name of the method, if you want to use/call the method, you have to know its name. Just like you want to make a call to a friend, you need to know his/her name.
  • list of parameters/arguments -- it defines the input of the methods. In order to do some processing within a method, it often needs to take in some input from whoever that calls the method.
  • method body -- it defines the actual work to be done by the method.

Let's use method addNumber to understand each concept better :

public double addNumber(double x, double y) {
	double sum = x + y;
	return sum;
}
  • public -- Method modifier. It tells the compiler how to call the method. Always use public for now, you will learn more about this later on.
  • double -- The data type of the value returned by the method. In this example, x + y which is of double data type, will be returned.
  • addNumber -- Name of the method.
  • double x, double y -- The parameters that are passed to the method. In this example, there are 2 parameters: x and y. Both x and y have double data type.
  • double sum = x + y; return sum; -- The method body. The result of (x + y) is assigned to variable sum, and is returned by the method. The data type must match those stated at point 2.

Take a look at another method sayHello:

public void sayHello(String name) {
	System.out.println("Hello " + name);
}
  • void -- This means that the method will not return any value. Take note that there is no "return ..." in the method body.
  • System.out.println("Hello " + name); -- The method body. It prints out the value in its parameter.

Call a Method

Calling a method is to use the method. If Defining a method is like building a robot, calling the method would be like asking the robot to do work for you. The very purpose of defining a method is to call it to carry out some meaningful work (which is defined in the method body). when you call a method, it may be defined within the same class or in another class. Depending on where it is defined, the way you call it also differs.

Call a Method within the Same Class

Call a method within the same class is rather straightforward, you just use the method name. In the example given below, method callShow calls method show by using show();. Why there is nothing inside ()? Because the method showdoes not take in any parameter/argument. So if you want to call sayHello, simply writing sayHello(); would give an error since it demands a parameter/argument from the caller. So to call it, you need to provide a name String as the argument, like sayHello("John");.

public class MyClass {
	public void show() {
		System.out.println("this is a method");
	}
 
	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
 
	public double addNumber(double x, double y) {
		double sum = x + y;
		return sum;
	}
 
	public void callShow() {
		show();
	}
 
}

Call a Method of Another Class

Calling a method of another class is more complicated. Usually you need to perform three steps:

  1. Create a object of the class which the method belongs to.
  2. Assign the object to a variable reference.
  3. Call the method by the variable.

Using the following code as an example. If we want to call method show in class MyCLass, we need to

  1. new MyClass() -- Create a object of the class MyClass
  2. MyClass c=new MyClass(); -- Assign the object to a variable reference c
  3. c.show(); -- Call the method by the variable c
public class YourClass {
	public void callAnother() {
		MyClass c=new MyClass();
		c.show();
	}
 
}

Note 1: When we call a method, we need to provide the method with the correct arguments. In the previous case, there is no argument provided because method show does not require any argument. If we want to call sayHello, according to its definition, we need to provide one argument of type String. An example is given below,

public class YourClass {
	public void callAnother() {
		MyClass c=new MyClass();
		c.sayHello("John");
	}
 
}

A Special Method

To be added later.

Last modified: Saturday, 24 January 2015, 10:36 AM