Declaring Methods


Declaring Methods

The general syntax for a method declaration is ”

 [modifiers] return_type method_name(parameter_list)                [throws exception1, exception2, ...]  {    //  body of method } 

The first part of a method declaration is called the header. The header tells the compiler the method name, its return type, any special keywords applied to the method, and the method's parameter list. The header must include the method name and return type. The other parts are optional. Following the header, a method declaration must include a pair of parentheses and a body. For abstract methods the body must be an empty statement, represented by the semicolon. For all other types of methods, the body is a block of code, which may be just an empty pair of braces, {} . The modifiers that can be applied to a method include access modifiers and other keywords ( abstract , final , static , synchronized , native ) that define any special characteristics of the method. The header may include a throws clause that declares the exceptions that the method can throw. Exception handling is discussed in more detail in Chapter 12.

A method can return one and only one value. The return type specified in the method declaration defines the type of value that will be returned. The return type can be any primitive or reference type defined in the Java language. If a method does not return a value, the return type is void . When a method specifies a reference type as its return type, the return value can either be an instance of that type or an instance of a subclass of that type. For example, a method with a return type of Object could return any reference type since Object is the ancestor of all other Java classes.

The parameter list defines the number and type of arguments that will be passed to the method. The input parameters are given names that the method will use when referring to them. A method that takes multiple parameters separates them with commas in the parameter list.

Example: Declaring Methods

The DeclareDemo class declares two methods. The getHeight() method is an instance method that returns the value of a variable named height . The getDateTime() method is a static method that returns the date and time that the method was invoked. Note that the instance and static methods are called in different ways. This is explained in more detail in the "Instance Methods" and "Static Methods" sections later in this chapter. The DeclareDemo class also defines a constructor. Constructors are discussed in Chapter 7.

 import java.util.*; public class DeclareDemo {   private double height;   // The DeclareDemo constructor initializes the height   // field   public DeclareDemo(double h) {     height = h;   }   // Three methods are declared.   public double getHeight() {     return height;   }   public static String getDateTime() {     Date date = new Date();     return date.toString();   }   public static void main(String args[]) {     DeclareDemo demo = new DeclareDemo(2.3);     System.out.println("height is "+                           demo.getHeight());     System.out.println("date and time are " +                           DeclareDemo.getDateTime());   } } 

Output ”

 height is 2.3 date and time are Sat Aug 10 19:09:41 GMT-7:00 2002 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net