Java Tutorial: Methods

  1. Modularity
  2. Implementing Methods
  3. Method Declaration
  4. Passing Data Between Methods
  5. The Java API Specification
  6. Methods in java.lang.Math

    1. Modularity
    2. One principle in software engineering is that of modularity. This means that programs should be broken down into small, managable chunks. In Java, this means using classes and methods. Classes are bigger program units that contain methods. As you have already seen, one class can be a complete application program, as long as it has a method called main. A class can also be a complete applet, and it will contain methods such as paint and init. It is also possible for an application program or an applet to contain many classes.

      Inside a class, there may be many methods. Consider the class called Person, shown in Figure 1. This class contains methods for different actions a person might do. These include eat, sleep, talk, shop, lift, and run. This is an example of modularity. Having different modules allows code to be reused. For example, suppose the Person wishes to eat three times in a day; the method eat can be called three times. This also allows methods such as main to be shorter and more readable.

      You may view and run the code for the Person class here.

      Figure 1: Person Class
      Figure 1: The Person Class


      back to top

    3. Implementing Methods
    4. This figure shows the shop method in the Person class.

      A method has two major parts: method declaration and method body. The method declaration defines things such as the access level, modifier, return type, method name, and the parameter list.

      The method body contains the Java instructions that the method must execute.


      back to top

    5. Method Declaration
    6. For now, our method declarations will contain

      1. access level,
      2. modifier,
      3. return type,
      4. method name,
      5. and the parameter list.

      The access level controls which classes can access the method, like a security level. It can be public, private or protected. A method with public access can be accessed by an outside class. A method with private access can only be accessed by the class it is defined in. A method with protected access can only be accessed by super classes and subclasses of the class it is defined in. For now all your methods will have a public access level.

      The modifier is an optional part of the declaration that sets certain properites for the method. The modifier can include abstract, final, native and synchronized but we will almost always use static. static declares this method as a class method rather than an instance method. (This will make more sense after you learn more about Object-Oriented programming.)

      The return type is the type of variable the method returns to the method that called it. The method shop() shown above, returns output which has type String. A method can return a variable of any of the primitive types (int, short, long, byte, char, float, double, Boolean) or of any class type (such as String). However, a method can only return one item to its calling method. (That is, it cannot return a float and a String). Read Java Tutorial on Returning a Value from a Method.

      The method name is a name given to the method by the programmer. The name should summarize what the method does. It must be one word with no whitespace. If you wish to assign a name with more than one word in it, then start words with a capital (except the first word). Some examples of good method names are: sortList, onePlayerGame, getUserName.

      The parameter list is used to pass information into the method. You must declare the number and type of the parameters required by that method. Read Java Tutorial on Passing Information into a Method.

      For a more detailed look at method definition, go to the creating methods tutorial..


      back to top

    7. Passing Data Between Methods
    8. Methods receive information from other methods through their parameter list. After processing that information, methods return information to the calling method through the return type. Cashier.java is a cashier program that calculates the change to give a customer who buys one item. Download, examine and run the code. In class, draw a flow chart showing the program flow.


      back to top

    9. The Java API Specification
    10. Java has a large library of prewritten code called the Java Applications Programming Interface or Java API. Oracle hosts a detailed description of the API at https://docs.oracle.com/javase/7/docs/api/. The code is made available to programmers so that commonly used functionality does not need to be written over and over again. As a Java programmer, it is important that you become familiar with the API, how to use it, and how to look up the use of classes and methods in the library.


      back to top

    11. Methods in java.lang.Math
    12. One class described in the Java API is java.lang.Math. To find the description of the Math class, go to the Java API and click on java.lang in the top left frame. All the classes found in java.lang will then appear in the bottom left frame. Click on the class called Math. The main frame will then display the description of the Math class.

      Scroll down in the main frame and find a table titled method summary. Here you will find a number of useful mathematical functions such as sin, cos, tan, max, min, round, pow, random and sqrt.

      Have a look at MathClassSampler.java for examples of how to use tan, max, round, and sqrt.


      back to top

    ©2004 Mount Douglas Secondary Information Technology Department  |   mdinfotech.net