Adding Some Methods to Your Madness
In Java, a method is a block of statements that has a name and can be executed by calling (also called invoking) it from some other place in your program. You may not realize it, but you're already very experienced with using methods. For example, to print text to the console, you use the println or print method. To get an integer from the user, you use the next Int method. And to compare string values, you use either the equals method or the equals Ignore Case method. And the granddaddy of all methods, main, is the method that contains the statements that are executed when you run your program.
All the methods you've used so far (with the exception of main) have been methods that are defined by the Java API and that belong to a particular Java class. For example, the nextInt method belongs to the Scanner class, and the equalsIgnoreCase method belongs to the String class.
In contrast, the main method belongs to the class defined by your application. In this chapter, you find out how to create additional methods that are a part of your application's class. You can then call these methods from your main method. As you'll see, this method turns out to be very useful for all but the shortest Java programs.
The Joy of Methods
The use of methods can dramatically improve the quality of your programming life. For example, suppose the problem your program is supposed to solve is complicated and you need at least 1,000 Java statements to get ‘er done. You could put all those 1,000 statements in the main method, but it would go on for pages and pages. It's better to break your program up into a few well-defined sections of code and place each of those sections in a separate method. Then your main method can simply call the other methods in the right sequence.
Or suppose your program needs to perform some calculation, such as how long to let the main rockets burn to make a mid-course correction on a moon flight, and the program needs to perform this calculation in several different places. Without methods, you'd have to duplicate the statements that do this calculation. That's not only error-prone, but makes your programs more difficult to test and debug. But if you put the calculation in a method, you can simply call the method whenever you need to perform the calculation. Thus methods help you cut down on repetitive code.
Another good use for methods is to simplify the structure of your code that uses long loops. For example, suppose you have a while loop that has 500 statements in its body. That makes it pretty hard to track down the closing brace that marks the end of the body. By the time you find it, you probably will have forgotten what the while loop does. You can simplify this while loop by placing the code from its body in a separate method. Then all the while loop has to do is call the new method.
TECHNICAL STAUFF | At this point, the object-oriented programming zealots in the audience are starting to boo and hiss. A few of them have already left the auditorium. They're upset because I'm describing methods in traditional procedural-programming terms instead of modern object-oriented programming terms. |
Well, phooey. They're right, but so what? I get to the object-oriented uses for methods in Book III. There, you find out that methods have a far greater purpose than simply breaking a long main method into smaller pieces. But even so, some of the most object-oriented programs I know use methods just to avoid repetitive code or to slice a large method into a couple of smaller ones. So there.
No comments:
Post a Comment