Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following figure shows the code forStack
'spush
method. This method puts the object argument onto the top of the stack and returns the object.Similar to a class, a method declaration names the method's access level, return type, name, arguments, and the method body, as shown in the previous figure. The method body is where all the action takes place. It contains the instructions that implement the method. The following figure names some of the elements of the The
push
Method and the Structure of a Method Definitionpush
method.The only required elements of a method declaration are the method's return type, name, and a pair of parentheses, Components of the Declaration of the
push
Method(
and)
. The next table shows the most commonly used elements of a method declaration.Each element of a method declaration can be further defined and is discussed as indicated in the following list:
Method Declaration Elements Element Function @annotation
(Optional) An annotation (sometimes called meta-data) accessLevel
(Optional) Access level for the method static
(Optional) Declares a class method <TypeVariables>
(Optional) Comma-separated list of type variables. abstract
(Optional) Indicates that the method must be implemented in concrete subclasses. final
(Optional) Indicates that the method cannot be overridden native
(Optional) Indicates that the method is implemented in another language synchronized
(Optional) Guarantees exclusive access to this method returnType methodName
The method's return type and name ( paramList )
The list of arguments to the method throws exceptions
(Optional) The exceptions thrown by the method Two of these components comprise the method signature: the method's name and the parameter list.
@annotation
- Annotations provide a mechanism for annotating program elements with meta-data. Some annotations affect how many warnings the compiler issues; some annotations are used by other tools and represent data auxiliary to the program. For more information, see Annotations.
- accessLevel
- As with fields, you control which other classes have access to a method using one of four access levels: public, protected, private, and default (also known as package private). The Controlling Access to Members of a Class section covers access levels in detail.
static
- As with fields,
static
declares this method as a class method rather than an instance method. The Understanding Instance and Class Members section talks about declaring instance and class methods.<TypeVariables>
- Used when declaring a generic method. Generic methods are used less frequently than generic classes and mostly used when creating library methods to deal with generic classes. In many situations, the compiler will handle generic methods transparently by way of type inference. For now, you shouldn't worry too much about this as it will be covered in more detail in Generics.
abstract
- An abstract method has no implementation and must be a member of an abstract class. Refer to the Writing Abstract Classes and Methods section for information about why you might want to write an abstract method and how such methods affect subclasses.
final
- A final method cannot be overridden by subclasses. The Writing Final Classes and Methods section discusses why you might want to write
final
methods, how they affect subclasses, and whether you might want to write afinal
class instead.native
- If you have a significant library of functions written in another language, such as C, you may wish to preserve that investment and to use those functions from a program written in the Java programming language. Methods implemented in another language are called native methods and are declared as such using the
native
keyword.synchronized
- Concurrently running threads often invoke methods that operate on the same data. Mark these methods with the
synchronized
keyword to ensure that only one thread at a time can access them. Synchronizing method calls is covered in Threads: Doing Two or More Tasks at Once. Take particular note of the section Synchronizing Threads.- returnType
- A method must declare the data type of the values that it returns. If your method does not return a value, use the keyword
void
for the return type. The Returning a Value from a Method section talks about the issues related to returning values from a method.- methodName
- A method name can be any legal identifier. You need to consider code conventions, name overloading, and method overriding when naming a method. These topics are covered in the next section Naming a Method.
( paramlist )
- You pass information into a method through its arguments. Empty parentheses indicate that this method has no parameters. See the Passing Information into a Method or a Constructor the following section.
throws exceptionList
- If your method throws any checked exceptions, your method declaration must indicate the type of those exceptions. See Handling Errors Using Exceptions for information. In particular, refer to Specifying the Exceptions Thrown by a Method.
Naming a Method
Although a method name can be any legal identifier, code conventions restrict method names. In general, method names should be a word or phrase that begins with a verb and should be in mixed case, with the first letter in lowercase and the first letter of each internal word in uppercase. Here are some examples:A method name should not be the same as the class name, because constructors are named for the class.run runFast getBackground compareTo setX getXTypically, a method has a unique name within its class. However, three situations might cause a method to have the same name as other methods in the class or in a superclass: overriding methods, hiding methods, and name overloading.
Note: More information on naming conventions can be found in The Java Language Specification by James Gosling, Bill Joy, Guy Steele and Gilad Bracha.A method with the same signature as a method in a superclass overrides or hides the superclass method. The Overriding and Hiding Methods section describes what each means, shows you how to override and to hide methods, and discusses related issues.
The Java programming language supports name overloading for methods, which means that multiple methods in the same class can share the same name if they have different parameter lists. Suppose that you have a class that can draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. In other languages, you have to think of a new name for each method, for example,
drawString
,drawInteger
,drawFloat
, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different type of argument to each method. Thus, the data drawing class might declare three methods nameddraw
, each of which takes a different type of argument.Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample,public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } }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.
Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback FormCopyright 1995-2005 Sun Microsystems, Inc. All rights reserved.