No statement outside the method in which a parameter variable is declared
Here is an example of a typical method declaration: Show
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here } The only required elements of a method declaration are the method's return type, name, a pair of parentheses, More generally, method declarations have six components, in order:
Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson. Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types. The signature of the method declared above is: calculateAnswer(double, int, double, double) Naming a MethodAlthough 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 MethodsThe 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"). Suppose that you have a class that can use calligraphy
to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } } Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, 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. Class declarations define new reference types and describe how they are implemented (§8.1). A top level class is a class that is not a nested class. A nested class is any class whose declaration occurs within the body of another class or interface. This chapter discusses the common semantics of all classes - top level (§7.6) and nested (including member classes (§8.5, §9.5), local classes (§14.3) and anonymous classes (§15.9.5)). Details that are specific to particular kinds of classes are discussed in the sections dedicated to these constructs. A named class may be declared Classes may be decorated with annotations (§9.7) just like any other kind of declaration. The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors (§8.1.6). The scope
(§6.3) of a member (§8.2) is the entire body of the declaration of the class to which the member belongs. Field, method, member class, member interface, and constructor declarations may include the access modifiers
(§6.6) Field declarations (§8.3) describe class variables, which are incarnated once, and instance variables, which are freshly incarnated for each instance
of the class. A field may be declared Member class declarations (§8.5) describe
nested classes that are members of the surrounding class. Member classes may be Member interface declarations (§8.5) describe nested interfaces that are members of the surrounding class. Method declarations (§8.4) describe code that may be invoked by method invocation expressions
(§15.12). A class method is invoked relative to the class type; an instance method is invoked with respect to some particular object that is an instance of a class type. A method whose declaration does not indicate how it is implemented must be declared Method names may be overloaded (§8.4.9). Instance initializers (§8.6) are blocks of executable code that may be used to help initialize an instance when it is created (§15.9). Static initializers (§8.7) are blocks of executable code that may be used to help initialize a class. Constructors (§8.8) are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances. Like methods, they may be overloaded (§8.8.8). 8.1. Class DeclarationsA class declaration specifies a new named reference type. There are two kinds of class declarations: normal class declarations and enum declarations. The rules in this section apply to all class declarations, including enum declarations. However, special rules apply to enum declarations with regard to class modifiers, inner classes, and superclasses; these rules are stated in §8.9. The Identifier in a class declaration specifies the name of the class. It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces. The scope and shadowing of a class declaration is specified in §6.3 and §6.4. 8.1.1. Class ModifiersA class declaration may include class modifiers. ClassModifier: (one of) The rules for annotation modifiers on a class declaration are specified in §9.7.4 and §9.7.5. The access modifier The access modifiers The
modifier It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration. If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier. 8.1.1.1. |