This link has been bookmarked by 14 people . It was first bookmarked on 18 Nov 2006, by Danny armstrong.
-
24 Aug 09
-
method declaration are the method's return type, name, a pair of parentheses,
(), and a body between braces,{}. -
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
-
calculateAnswer(double, int, double, double)
-
the first letter of each of the second and following words should be capitalized. Here are some examples:
-
a method might have the same name as other methods due to method overloading.
-
This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").
-
Thus, the data drawing class might declare four methods named
draw, each of which has a different parameter list. -
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
-
Note: Overloaded methods should be used sparingly, as they can make code much less readable.
-
-
21 Mar 09
-
Two of the components of a method declaration comprise the method signature—the method's name and the parameter types
-
Two of the components of a method declaration comprise the method signature—the method's name and the parameter types
-
Two of the components of a method declaration comprise the method signature—the method's name and the parameter types
-
method overloading
-
This means that methods within a class can have the same name if they have different parameter lists
-
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
-
-
10 Feb 09
-
Defining Methods
<!---Defining Methods---> Here is an example of a typical method declaration:
The only required elements of a method declaration are the method's return type, name, a pair of parentheses,public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here }(), and a body between braces,{}. -
- Modifiers—such as
public,private, and others you will learn about later. - The return type—the data type of the value returned by the method, or
voidif the method does not return a value. - The method name—the rules for field names apply to method names as well, but the convention is a little different.
- The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses,
(). If there are no parameters, you must use empty parentheses. - An exception list—to be discussed later.
- The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
More generally, method declarations have six components, in order:
- Modifiers—such as
-
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
-
Naming a Method
Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:run runFast getBackground getFinalData compareTo setX isEmpty
-
Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.
-
Overloading Methods
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").
-
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample,
draw(String s)anddraw(int i)are distinct and unique methods because they require different argument types.You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
-
Note: Overloaded methods should be used sparingly, as they can make code much less readable.
-
-
09 Oct 08
-
27 Mar 07
-
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures.
-
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
-
Overloaded methods are differentiated by the number and the type of the arguments passed into the method.
-
-
18 Nov 06
-
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
-
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample,
draw(String s)anddraw(int i)are distinct and unique methods because they require different argument types.
-
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.