Google
    Custom Search

    How to Write and Use a Java Method

    by Nancy Sewell

    Simple Java Method

    Sometimes the terms function and method are used interchangeably. They are virtually the same. The correct terminology for Java is method. It is a set of commands that can be used over again. In this, they share similarities with sub routines in the early days of programming.

    Things You'll Need:

    • Java Compiler
    • Very basic Java knowledge

    1.
    A simple Java method requires a minimum of three items:

    Visibility : public, private, protected
    Return Type: void, int, double, (etc.)
    name: whatever you want to call the method

    Visibility means who can access it. If it is public, anyone who has access to your class file can access the method. In some circumstances, this is perfectly fine. If the method is private, only the functions inside of that class can call the method. This is used as utility methods to do something you do not want just anyone who uses the class to do. Protected gives public function to all child classes.

    Return type is void if you do not want the method to give you any data back. It would be used for such things as a method that prints out something. Any other return requires a return statement with the type of data it returns. For example, if you add two integers and want the results of that integer, your return type would be int.

    Name of the method is anything you choose that is not already used in the class (unless you are overloading the method which is beyond the scope of this article)

    If you want the method to do something with the data you supply it, you also need to include parameters within the ( ) You include what data type it is and give that parameter a name (ie: you are declaring a local variable for that method only)

    2.
    examples:

    access   return type   name   parameters
    public     void             add     (int a, int b)

    Java method example

    This would be written

        public void add(int a, int b)
         {
          // do stuff here
         }

    Since the return type is void, you will have to write what you want the method to do inside of the method such as by printing it out from the method

        public void add(int a, int b)
         {
          System.out.println(a+b);
         }

    To use the above method, you would simply call that method and inserting the two integers that will go into the integer parameters:

        add(7, 4);

    If we want to use the results for something else within our main method, we will need a return type. Since we are adding integers, int return type is adequate:

        public int add(int a, int b)
         {
          // do stuff here
          // return type required
         }

    Since we have an integer return type, we need to have a return statement that will return an int. We can use a local variable (c) to hold the result and return the value of that local variable. Note that c must be the same datatype as the return type - which is int in this example:

        public static int add(int a, int b)
         {
          int c = a+b;
          return c;
         }

    This can also be shortened to do away with the local variable:

        public static int add(int a, int b)
         {
          return a + b;
         }

    3.
    In order to use the above with the int return type, In the main method you can either set a variable to the result:

        int temp = add(5, 7);

    or you can print out the result by calling it in your print statement:

        System.out.println( add(3,3));
    
    
    I know this is very simple. I found that sometimes instructors fail to explain the simple things to students assuming they already know it. This is meant for beginner students who are not understanding the concept and is not meant for those who are already accomplished programmers.
    
    
    
    Return to HorsesNW.com

    Privacy Policy