![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |
A method's declaration provides a lot of information to the compiler, the runtime system and to other classes and objects about the method. Besides the name of the method, the method declaration carries information such as the return value of the method, the number and type of the arguments required by the method, and what other classes and objects can call the method.While this may sound like writing a novel rather than simply declaring a method for a class, most method attributes can be declared implicitly. The only two required elements of a method declaration are the method name and the data type returned by the method.
For example, the following code sample declares a method named
isEmpty()
within the Stack class.Theclass Stack { . . . boolean isEmpty() { . . . } }isEmpty
method returns a boolean value (true or false).The next two sections cover what you need to know to name your method and to return a value from it. The section after that covers the more advanced features of method declarations such as controlling access to methods.
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, the
isEmpty()
method in the Stack class returns a simple data type, a boolean value:However, 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; } }pop()
method in the Stack class returns a complex data type: an object.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
operator returns the value and must be the last statement in the method. Any method that is not declaredvoid
must contain areturn
statement.The data type of the value returned by the
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 type, the returned object must either derive from, or be the class indicated. When returning an interface type, the object returned must implement the specified interface.
A Method's Name
A method name can be any legal Java identifier. There are three 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 (strings, integers, and so on) 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:Note: The information within the ( and ) in the method declaration are arguments to the method. Arguments are covered later in Passing Information into a Method.class DataRenderer { void draw(String s) { . . . } void draw(int i) { . . . } void draw(float f) { . . . } }The methods are differentiated by the compiler by the number and type of the arguments passed into the method. Thus,
draw(String s)
anddraw(int i)
are distinct and unique methods. You cannot declare more than one method with the same signature:draw(String s)
anddraw(String t)
are identical and will result in a compiler error. You should note that overloaded methods must return the same data type; thusdraw(String s)
andint draw(String t)
declared in the same clas will produce a compile-time error.- Any method whose name is the same as its class is a constructor and has a special duty to perform. Constructors are used to create and initialize a new object of the class type. The only way to create an object is through one of its constructors; constructors can only be called with Java's
new
operator. For more information about how to create an object, see Creating an Object. For more information about how to write a constructor, see Writing a Constructor Method.- [PENDING: Should also mention overriding methods here and link to other places.]
Other Method Declaration Stuff
Now that you understand the two required components of a method declaration, we can start investigating the more advanced features of a method declaration. The first four links below are in-line in this lesson. If you use the next and previous links at the top and bottom of each page in this lesson, you will ultimately see those four sections.The last three links cover topics that either warranted their own lesson, or were already critical to another lesson. Choosing those links will take you to a different part of this tutorial. Be sure to come back!
- Within a method declaration, you can use access specifiers to control which other objects and classes in your program can call your method. See Controlling Access to Methods.
- You can also specify whether the method is an instance method or a class method. Click here for The Scoop on Instance and Class Methods.
- You pass information into a method through its arguments. See Passing Information into a Method.
- When you are writing a class, your method may have to provide information about that method to any subclasses of your class, such as whether the method can be overriden, or whether your class even provides an implementation for the method. See Subclasses, Superclasses, and Inheritance for information about how subclassing your class can affect your method declarations.
- If you method throws any exceptions, your method declaration must indicate which exceptions your method can throw. See Handling Errors using Exceptions
for more information. In particular, refer to the page Declaring the Exceptions Thrown by a Method
![]()
- If you have a significant library of functions written in another language such as C, you may wish to preserve that investment and use those functions from Java. Methods implemented in a language other than Java are called native methods and must be declared as such within the method declaration. To learn how to integrate Java code with code written in other languages, see Integrating Native Methods into Java Programs
![]()
- Concurrently running threads often call methods that operate on the same data. These methods must be synchronized to ensure that the data remains in a consistent state throughout the life of the program. You can declare that a method must be synchronized with the
synchronized
keyword. However, there is more to it than that. Synchronizing method calls is covered in the Threads of Controllesson. Take particular note of the page titled Synchronization
![]()
Summary of a Method Declaration
In summary, a method declaration looks like this:[PENDING: make links of the above][accessSpecifier] [static] [abstract] [final] [native] [synchronized] returnType methodName ([paramlist]) [throws exceptionsList]
![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |