Website Design United States, Website Design California, Website Designing United States, Website Designing California

A Closer Look at Methods in Java

In object-oriented programming, a behavior is referred to as a message that one object sends to another object. Behavior is the only way by which an object can do anything.

Implementing Methods

A method contains executable code that can be invoked by passing a fixed number of values as arguments. A method has two major parts:

method declaration

method body
A method declaration gives a lot of information about the method to the compiler, to the runtime system, and to other classes and objects. In addition to the name of the method, the elements in a method declaration are as follows:

access modifier

additional modifiers

return type

a formal parameter list

checked exceptions thrown by the method
A method body contains a block of code that implements the method. It specifies those statements that execute when the method is called. All the actions take place within the method body.

The general syntax used to write a method is given below:

[access modifiers] [method modifiers] return type method name ([formal parameter list]) [throws clause]{
// Method body
}

where, the elements given within square braces are optional. The only required elements to declare a method are the name of the method, a return type of the method, and a pair of parentheses.

Access Modifiers used in a Method Declaration

The use of an access modifier in a method declaration determines which other classes can access the method. A method can be declared with one of the following access modifiers: public, private, and protected.

public

When a method is declared as public, code contained in its defining class and every other class in the same package can call the method. If the class in which the method is defined is also declared as public, code in every class whether in the same package or any other package can call the method.

protected

When a method is declared as protected, code contained in its defining class, all classes in the package in which the class that defines the method belongs to, and all subclasses of that class (regardless of package) can call the method.

private

When a method is declared private, only code contained in its defining class can call the method. Code in any other classes cannot call the method.

No access modifier

When none of the three access modifiers is used to declare a method, the "package access" also called "default access" applies. The default access causes the method to be callable from code within its defining class and from all classes within the same package. The method cannot be called by any other class that is not declared in the same package in which the class that declares the method is defined.

Method Modifiers

The static modifier

The use of the static modifier in a method declaration expression declares the method as a class method rather than an instance method. Methods declared with the keyword static as a modifier are called static methods or class methods. They are so called because they affect a class as a whole, and not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class.

A static method suffers from the following restrictions:

A static method can only call other static methods.

A static method must only access static data.

A static method cannot reference to the current object using keywords super or this.

If a method is not declared as static, it is called an instance method. Instance methods are associated with objects and not with classes. Furthermore, they can access all the variables in a class whether they are instance fields or class fields.

The abstract modifier

An abstract method is a method without implementation, i.e., an abstract method is not defined in the class in which it is declared. The following points must be noted about the declaration of a method:

An abstract method declaration provides the signature of the method, return type, and throws clause (if any), but it does not provide an implementation of the method. The declaration of an abstract method ends with a semicolon rather than a block of code.

The class in which the abstract method is declared must itself be declared as abstract.

The use of the abstract keyword in a method declaration along with the modifiers: final, static, native, synchronized, or private is not permitted.

By default, a method is non-abstract and it requires a block of code after the declaration part.

The final modifier

When a method is declared as final, it cannot be overridden or hidden by a subclass. In other words, a subclass cannot introduce a new version of the method. Methods declared as private and all the methods declared in a final class are implicitly final and there is no need to explicitly declared them as final.

The native modifier

Methods implemented in a language other than Java are called native methods. These methods are declared by using the native keyword. The body of a native method is replaced by a semicolon, which indicates that the implementation of the method is omitted. When a method is declared as native, the JVM arranges for a native method call to result in execution passing to native library code.

The synchronized modifier

The use of the synchronized keyword in a method declaration ensures that at a time only one thread can execute the block of statements following the method declaration. Other threads wanting access to the method are forced to wait until the currently executing thread returns from the method.
The Role of a return type in a Method Declaration

Java requires that a method must be declared with the data type of the value that it returns. A method declaration specifies the type of value that the method returns. If a method does not return a value, the keyword void is used, which indicates that the method does not return a value.

Method signature

The combination of the name of the method and formal parameter list forms the method signature.

Method name

A method name can be any legal Java identifier.

Formal parameters

The formal parameters of a method are specified by a list of comma-separated parameters specifiers. The general form of a parameter specifier is shown below:

[final] type parameter_name

Each parameter specifier consists of its type and name. The use of the final modifier in a parameter declaration is optional. If a method has no parameters, an empty pair of parentheses appears in the declaration of the method.

Parameters are considered local to a method. The scope of a parameter of a method is the entire body of the method. The parameter is created each time a method is called and is destroyed when the execution leaves the method and returns to the caller of the method.

When the method is invoked, the values of the actual arguments initialize newly created parameter variables before the execution of the body of the method. Therefore, a compile-time error will occur if a method parameter that is declared final is assigned a value within the body of the method.
The throws exceptions clause in a method declaration

A throws clause is used in a method declaration to declare any checked exceptions that can result from the execution of a method. If a method throws any checked exceptions, the method declaration must indicate the type of those exceptions by using a throws statement followed by a comma-separated list of the checked exception. Although it is not required to mention other (unchecked) exceptions in a throws clause, they can be placed in the throws clause.

The throws clause of a method mentions the exception type or a superclass of that exception for each checked exception that can result from execution of the body of a method. Failure to do so will result in a compile-time error.

Overloading methods

In Java, each method has a signature, which comprises the name of the method and the types and order of the parameters in the formal parameter list.

Java supports method name overloading, i.e., within a class, it possible to declare multiple methods with the same name as long as they differ in their parameter declarations. When this is the case, the method name is said to be overloaded, and the process is called method overloading.

For example, the class java.lang.Math class contains an overloaded method named abs, which returns the absolute value of the argument. The signatures of different versions of the method are given below:

public static int abs(int num)
public static long abs(long num)
public static float abs(float num)
public static double abs(double num)

When an overloaded method is invoked, Java uses either the type of the argument or the number of the argument, or both as its guide to determine which version of the overloaded method to execute. This means that overloaded methods must differ either in the type of parameters or number of their parameters, or both.

The Method body

A method body is a block of code that implements the method. However, if the method is either declared as abstract or native, the block of code is replaced by a semicolon, which indicates an unimplemented method. The body of an implemented method specifies those statements that execute when the method is invoked. Besides other executable statements, the body of a method may contain the following elements:

The return statement

A return statement is used within a method body to return a value from a method. Methods declared with a return type other than void return a value to the calling code using the following form of the return statement:

return expression;

where, the expression is a value returned. If a method is declared as void, its body does not contain any return statement that has an expression.

There are two points to be noted about returning a value from a method:

The data type of the value after the return statement must match the return type of the method. For example, if a method is declared to return a boolean type, only the boolean values true or false may be placed after the return statement.

The variable receiving the value returned by a method must be compatible with the return type with which the method was declared.
Apart from primitive types, a method may also return objects. When a method returns an object, the class of the returned object must be either a subclass of or the exact class of the return type.

The this keyword

In many situations, a method may need to refer to the object that invoked it. Java defines a keyword named 'this' to accomplish the task. The this keyword can be used within the body of a method to refer to the current object. The current object is the object on which the method was invoked.

The super keyword

If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.

Local variables

Variables declared within the body of a method are known as local variables. They are so called because they are not available for use outside the block where they are declared. These variables are temporary variables, which are visible to the program only within the scope of the method. The scope defined by a method begins with the block that follows the method declaration. If the method has parameters, they too are included within the scope of the method. When the program control leaves the block, all the variables in the block cease to exist.

About the Author:

uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/

Author Information:

Anonymous

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.

 















 


© 2008-2009 dotnet4all.com