Java Reference
In-Depth Information
There is a hidden danger in using static import declarations in such cases. Suppose you did not have a test()
static method in the p2.Test class. In the beginning, the test() method call will call p1.A.test() method. Later,
you add a test() method in the Test class. Now the test() method call will start calling p2.Test.test() , which will
introduce a hard-to-find bug in your program.
It may seem that static imports help you use simple names of static members to make the program simpler to
write and read. sometimes static imports may introduce subtle bugs in your program, which may be hard to debug. You
are advised not use static imports at all, or only in very rare circumstances.
Tip
Declaring Methods of a Class
A method in a class defines the behavior of the objects of that class or the behavior of the class itself. A method is a
named block of code. The method can be invoked to execute its code. The code that invokes the method is called
the caller of the method. Optionally, a method may accept input values from the caller and it may return a value to
the caller. The list of input values is known as parameters. A method may have zero parameters. If a method has zero
parameters, you say that method does not have any parameters or method does not take any parameters. A method is
always defined inside the body of a class. To keep the sample code simple, I will show a method as an isolated block of
code in this section. I will show a method inside a class body when I discuss a complete example.
The general syntax for a method declaration is of the form
<<modifiers>> <<return type>> <<method name>> (<<parameters list>>) <<throws clause>> {
// Body of the method goes here
}
Here, <<modifiers>> is an optional list of modifiers; <<return type>> is the data type of the value returned
from the method; <<method name>> is the name of the method. The method name is followed by a pair of opening
and closing parentheses. Optionally, you can specify one or more parameters to the method within the parentheses.
Multiple parameters are separated by a comma. The closing parenthesis may optionally be followed by a throws
clause. Finally, you specify the code for the method inside opening and closing braces.
Note that four parts in a method declaration are mandatory: the return type, method name, a pair of opening
and closing parentheses, and a pair of opening and closing braces. Let's discuss each part in a method declaration in
detail. I will discuss modifiers in various sections of this chapter and subsequent chapters in this topic. I will discuss
the throws clause in the chapter on exception handling.
The following is an example of a method: it is named add ; it takes two parameters of type int named n1 and n2 ,
and it returns their sum:
int add(int n1, int n2) {
int sum = n1 + n2;
return sum;
}
The return type of a method is the data type of the value that the method will return when it is invoked. It could
be a primitive data type (for example, int , double , boolean , etc.) or a reference type (for example, Human , String , etc.).
Sometimes, a method does not return a value to its caller. The keyword void is used as the return type if a method
does not return any value to the caller. In the above example, the add method returns the sum of two integers, which
will be an integer. This is the reason that its return type is specified as int .
 
 
Search WWH ::




Custom Search