Creating Methods in Java

Creating Methods in Java

In JavaScript, we created functions; in Java, everything is object-oriented, so we'll be creating methods. A method is just a function that's part of a class or object. As an example, I'll create a method now named adder that will add two integers and return their sum.

To start, I'll need two numbers to add, and I'll let the user enter them as command-line arguments. I can read those arguments from the array passed to the main method, which I name args , and store them in integers value1 and value2 , like this:

 public class ch10_13  {     public static void main(String[] args)     {         int value1 = Integer.parseInt(args[0]);         int value2 = Integer.parseInt(args[1]);     .     .     . } 

Now I display those values, pass them to the adder method, and display the value that adder returned, like this:

 public class ch10_13  {     public static void main(String[] args)     {         int value1 = Integer.parseInt(args[0]);         int value2 = Integer.parseInt(args[1]);  System.out.println(value1 + " + " + value1 +   " = " + adder(value1, value2));  }     .     .     . } 

All that remains is to create the adder method. You can give methods access specifiers such as public or private . If you give it the access specifier public , the method is accessible outside the object or class. If you give it the access specifier private (which is the default if you don't use an access specifier), it's accessible only inside the object or class. If you use the protected keyword, the method is accessible in objects of the current class or in classes based on the current class. I'll use public here (we won't use the protected or private keywords in this book).

In addition, you must specify the return type of the value the method returns (you can use the keyword void if the method does not return a value). And you must give a comma-separated argument list for the method, giving the type of each argument in parentheses following the method's name (if the method takes no arguments, leave the parentheses empty). All of this gives us the following skeleton for the definition of adder :

 public class ch10_13  {     public static void main(String[] args)     {         int value1 = Integer.parseInt(args[0]);         int value2 = Integer.parseInt(args[1]);         System.out.println(value1 + " + " + value1 +         " = " + adder(value1, value2));     }  public static int adder(int int1, int int2)   {   .   .   .   }  } 

In the body of the method, I can refer to the two values passed using the names I've given them in the argument list, int1 and int2 . I add those values and return the result using the return statement:

Listing ch10_13.java
 public class ch10_13 {     public static void main(String[] args)     {         int value1 = Integer.parseInt(args[0]);         int value2 = Integer.parseInt(args[1]);         System.out.println(value1 + " + " + value1 +         " = " + adder(value1, value2));     }     public static int adder(int int1, int int2)     {  return int1 + int2;  } } 

Now the user can enter values to add on the command line, and the application will handle them without a problem:

 %java ch10_13 180 120  180 + 180 = 300 

Using the return Statement

You can use the return statement even in methods from which you don't return any value if you want to terminate execution and return from the method. Just use the return statement alone, without specifying any values to return.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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