Section 4.9. Using Methods: Toward Object-Oriented Programming

   

4.9 Using Methods : Toward Object-Oriented Programming

While the next chapter is really devoted to understanding OOP and planning OOP applications, this chapter would not be complete without a discussion of methods. This will give us a flavor for objects.

So far we have only discussed procedural programs; that is, programs that work from top to bottom, like a browser reads HTML. Once we're inside the main() method, the program executes top to bottom with no break or redirection. It has been very straightforward.

This approach has been very useful, but one can quickly see that it is not very extensible. Creating programs that had all of their code behavior written inline would be a nightmare to update later, and it would be extraordinarily difficult to reuse any of your code.

As in ColdFusion, there are a number of reasons to define methods to handle specific tasks. It encourages you to organize your code, and it makes it eas ier for you and others to read your work when well-named methods handle discrete, clear tasks .

Using class-specific methods also encourages object orientation. Just because we're writing Java code doesn't mean our code is object oriented. Methods allow you to define methods that get and set data in an object ( get is approximately like SELECT and set is approximately like UPDATE ). Many objects define private data, which means that only methods defined in the object's class can modify it.

4.9.1 Method Naming Conventions

Like data and classes in Java, there are naming conventions for methods. Typical conventions are as follows :

  • Do not use an underscore .

  • Use verbs or verb phrases to describe the operation the method represents.

  • Start your method name lowercase and then use an uppercase first letter for each subsequent word in your method name.

The following are examples of well-named methods:

  • getPrice()

  • square()

  • setProductID()

By contrast, the following are poorly named methods:

  • price()

  • Square()

  • set_product_id()

Let's take a look at how you define methods, and how you call them.

4.9.2 Declaring Methods

Methods are all defined the same way, whether they are built in or are defined by the programmer. Any class you write can define zero or more methods. What can be difficult to remember when making the transition to Java from a non-object-oriented background is that the order in which you define your methods is indifferent. This is perhaps similar to writing a user -defined function library in ColdFusion ”you can create a page called lib.cfm and write all of your application functions in there, then <cfinclude> it. When you call the function determines when it is processed in the application. This is how it works in Java.

All methods are defined in the same manner:

 modifiers   return_type   method_identifier ([arguments]) {      method_body } 

You can open the method body with curly braces on the first line or on their own line. It doesn't make a difference. Just do what you find more easily readable. Let's take a look at each of the parts of the method declaration.

The modifier first defines the access type for the method; the access type declares what the visibility of the method is. Possible values are public , private , protected , or default .

The second kind of modifier defines the method type. Possible values are static , final , and abstract .

Next, the method declaration states its return type. A method will typically perform some calculation or modify an object, and then it may return something. It might just perform some operation and not return a value at all. It might also return the result of a calculation, or return a modified object. A method can return only one value. As with a ColdFusion UDF, use the return keyword to send the value back to the calling method. The return type specifies the type of the value that the method will return, for instance, char , String , int , or the name of an object type such as File . return specifies a single value, or an expression, which will be evaluated before being returned. The keyword void indicates that the method does not return anything. The parentheses surrounding the return value or expression are optional. For example, both of the following are legal:

 return (x * x); // okay  return x*x; // okay 

Because a method returns only one value, the following is illegal:

 return(x*x, y+2); // illegal 

Next comes the method identifier. This is simply the name that you want to call the method.

Inside the parentheses you define the argument list that the method will accept. A method can accept zero or more arguments, each of which must be of a specified type. Arguments, as in ColdFusion functions, are changeable information that get passed into a method so that some processing can be done with them. If a method accepts no arguments, the parentheses are necessary anyway.

Arguments are supplied by writing first the type and then the identifier. Each set of type-identifier arguments must be separated by a comma:

 public void setAge(int age) { ...  public void checkTicket(String event, java.util.Date eventDate, int seatNumber) { ... 

We will look at parameters supplied to methods in more detail later, when we discuss objects and classes.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net