Java Objects |
As you know, objects have behaviour and that behaviour is implemented by its methods. Other objects can ask an object to do something by invoking its methods. This section tells you everything you need to know to write methods for your Java classes. For more information about how to call methods see Missing Page.At minimum a method declaration has a return type indicating the data type of the value returned by the method, a name, and a method body that contains the Java instructions that implement the method:
For example, the following is a method declaration of a simple method that returns a boolean value (true or false) indicating whether or not something is empty.return_type method_name() { . . . method body . . . }Alone this method has no meaning--is what empty? This is because this particular method declaration is not associated with any class. It is the combination of a class and a method that gives meaning to a method. In Java, methods are associated with classes or objects and operate on the class or object with which they are associated. The results you get from theboolean isEmpty() { . . . }isEmpty()
method only have meaning if the method is associated with a stack object, or an array object, or some other type of object that can contain objects or data.In Java every method must be within a class declaration. An earlier section, Declaring a Class, showed you the general outline of a class declaration:
Within the class you define all the variables and methods supported by that class. This page focuses on how to declare and implement methods for a class. The next page shows you how to declare your class's methods.class declaration { . . . class body . . . }You declare all of the methods that are a member of the class within the class body. Typically, you declare a class's methods after you declare its variables, although this is not required.
Let's put theclass declaration { . . . member variable declarations . . . method declarations . . . }isEmpty()
method in an appropriate class:Now, we can say that theclass stack { . . . boolean isEmpty() { . . . } }isEmpty()
methods returns true when the stack is empty and returns false otherwise.The method declaration shown above is a very basic method declaration. Methods have many other attributes such as parameters, access control, and so on.
Returning a Value from a Method
Java requires that a method declare the data type of the value that it returns. If a method returns no value, it can be declared to returnvoid
.There are two major categories of data types in the Java language: simple types and complex types. Simple data types are comprised of a single value and include integer numbers, floating point numbers, boolean values, and characters (a single character, that is). Complex types are comprised of multiple or complex values and, sometimes, the operations that can be performed on those values. Complex types include classes, interfaces, and arrays.
Methods can return either values of simple data types or of complex data types. For example, this method returns a simple data type, a boolean value:
Theclass Stack { static final int STACK_EMPTY = -1; Object stackelements[]; int topelement = STACK_EMPTY; . . . boolean isEmpty() { if (topelement == STACK_EMPTY) return true; else return false; } }return
operator returns the value and must be the last statement in the method. Any method that is not declaredvoid
must contain areturn
statement. Thereturn
operator places the indicated value on the stack where the calling method can then retrieve it.The following method returns a complex data type: an object.
The data type of the value returned by theclass Stack { static final int STACK_EMPTY = -1; Object stackelements[]; int topelement = STACK_EMPTY; . . . Object pop() { if (topelement == STACK_EMPTY) return null; else { return stackelements[topelement--]; } } }return
statement must match the data type that the method claims to return. For example, you can't return an Object from a method declared to return an integer. When returning an object the object must either derive from, or be the class indicated. If your method returns an interface type, the object returned must implement that interface.A Method's Name
A method name can be any legal Java identifier. There are two special cases to consider in regards to Java method names.
- Java supports method name overloading so multiple methods can share the same name. For example, suppose you were writing a class that can render various types of data to its drawing area. You would need to write a method that knew how to render each data type. In other languages, you would have to think of a new name for each method:
drawString()
,drawInteger()
,drawFloat()
, and so on. In Java, you can use the same name for all of the drawing methods but pass in a different type of parameter to each method. So, in your data rendering class, you can declare three methods nameddraw()
each of which takes a different type parameter:class DataRenderer { void draw(String s) { . . . } void draw(int i) { . . . } void draw(float f) { . . . } }
Note: The information within the ( and ) in the method declaration are parameters to the method. Parameters are covered later in this section in Creating an Object. For more information about how to write a constructor, see Writing a Constructor Method.A Method's Body
The method body is where all of the action of a method takes place. The method body contains all of the legal Java instructions that implement the method.
- Passing Information into and out of Methods (pass by reference)
- Using this & super
Other Method Stuff
Summary
[access_specifier] [static] [abstract] [final] [native] [synchronized] return_value methodname ([paramlist]) [throws exceptionsList]
Table of Contents |