Java Beginners - Java For Beginners Java Methods Rating: 0.0/5 (0 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java MethodsAs we saw in Introduction to Objects , a class can define both attributes and behaviors. Attributes are represented by instance variables; behaviors are defined by methods. Without methods, an object cannot do anything. It can only passively wait for other objects to manipulate its visible data. Some programming languages use passive data structures that do exactly this; they contain a cohesive set of data, but they have no behavior and must rely on other functions or subroutines to manipulate their data.
Fortunately, Java is an object-oriented programming language. Good object-oriented design calls for a class's data to be as hidden as possible. If a class's data is hidden, other classes cannot manipulate the data directly. If another class wants to update the data, it must request this by invoking a mutator or setter method.
Note: Data hiding is known as encapsulation and is an object-oriented best practice.
Method Invocation
When an object calls a method (either one of its own or another object's), the calling object is actually requesting that the receiving object perform some action; set a value, return a value, write to a file, or send an email, for example. This is also know as message passing; one object passes a message to a second object. The message contains the name of the method to execute on the receiving object and any required data that the receiving method requires. Data that are passed to a method are known as arguments; the required arguments for a method are defined by a method's parameter list. Let's look at some examples of method invocations: // Call method executeQuery() on myObject. myObject.executeQuery();
// Call method set() on date, passing it the // arguments Calendar.MONTH and 11. date.set(Calendar.MONTH, 11);
// Call static method parseInt on Integer, passing // it argument "32" and assigning the return // value to variable age. int age = Integer.parseInt("32");
Method Signature
The combined name and parameter list for each method in a class must be unique. The uniqueness of a parameter list takes the order of the parameters into account. So int myMethod (int x, String y) is unique from int myMethod (String y, int x ). Let's take a look at the general syntax for a method declaration: [modifiers] return_type method_name (parameter_list) [throws_clause] {
[statement_list] }
Everything within square brackets [] is optional. Of course, you don't include the square brackets in your code; they are included here only to indicate optional items. You can see that the minimal method declaration includes: - Return Type: The return type is either a valid Java type (primitive or class) or void if no value is returned. If the method declares a return type, every exit path out of the method must have a return statement.
- Method Name: The method name must be a valid Java identifier. See Java Variables for the rules for Java identifiers.
- Parameter List: The parentheses following the method name contain zero or more type/identifier pairs that make up the parameter list. Each parameter is separated by a comma. Also, there can be zero parameters.
- Curly Braces: The method body is contained in a set of curly braces. Normally, the method body contains a sequence of semicolon delimited Java statements that are executed sequentially. Technically, though, the method body can be empty.
Note: If a method is declared with the abstract modifier or is defined in an interface instead of a class, there will be no method body; instead a semicolon will follow the parameter list's parentheses. Other Modifiers There are a number of optional modifiers that you can use when declaring a method. They are listed in the following table: | Optional Modifiers | | Modifier | Description | | Visibility | Can be one of the values: public, protected, or private. Determines what classes can invoke the method. | | static | The method can be invoked on the class instead of an instance of the class. For example, String.valueOf(35) is calling valueOf on the String class instead of on any specific String object. Of course, static methods can still be called on object instances: myString.valueOf(35). | | abstract | The method is not implemented. The class must be extended and the method must be implemented in the subclass. | | final | The method cannot be overridden in a subclass. | | native | The method is implemented in another language. | | synchronized | The method requires that a monitor (lock) be obtained by calling code before the method is executed. | | throws | A list of exceptions thrown from this method. | 1 | 2 | 3 | Next >
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|