Part One - What are methods?

A method is a re-useable peice of code that takes input and gives output. Sometimes people refer to them as functions or sub-routines but in Java they're methods

In java a method takes arguments of a specific type and returns a value of a specific type.

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

Thats a lot to take in.. lets look at each part of this method.

public is an access modifier. Public means that any other class can use doSomeSweetAdditon().

static is the static modifier. Adding this to the method makes it a class method. We'll learn what that means in the next part.

int is the return type of the method. Anything you return in this method must be of type = int.

doSomeSweetAddition is the name of the method

(int a , int b) are the arguments, you can have as many as you want or none at all. Arguments are prefixed with thier type and comma seperated.

return specifies what will be returned.

Part Two - class as opposed to instance methods

Belive it or not we've been using both static and instance methods for weeks now. The main method is a speciall static method that the JVM looks for when we try to run an application. It is also a static method and void returning.

So whats the difference? We can use static methods without first creating a new object. The main method, Math.sqrt() and System.out.println() are all static methods . What do you thin the return types of those three are?

Instance methods need to have an instance of an object to be called on. When we make a new Scanner and then call it's .nextDouble() method. We are using a public method of a Scanner object that has return type double. If you don't know we can just look it up.

Part Three - creating public static methods

So we know how to make a method. But where in the class do we put it and how do we use it. Lets make a method that takes one String argument and prints it three times.

//class definition
public class Lesson6 {

  //heres the main method
  public static void main( String[] args ){
    printThreeTimes("Yo dog, I heard you like methods...");
    printThreeTimes("So we put some methods in your methods.");
  }
  
  // heres the mathod we're defining
  public static void printThreeTimes( String printMe ){
    for( int i = 0; i<3 ;i++){
      System.out.println(printMe);
    }
  }

//end class definition
}
        

As you can see the new public static method goes right inside of the class definition , right next to the main methods. They're both public static methods so it should make sense that they go next to each other

We call our method by just writing its name followed by parenthesis. Just like you would with any of the method's we've already used

Part Four - using public static methods

lets write a public static method as a class. Make a public static method called circumfrence that takes the radius of a circle and returns the circumfrence. Then calculate the circumfrence of circles with 3 and 7 inch radii.

Part Five - recursion

Being able to call a method from within itself can yeild some intresting results. Lets take te example of a function that returns the factorial of a number.

In case you forgot a factorial of a number n is the product of all the positive integers less than or equal to it. So.. !5 = 5 x 4 x 3 x 2 x 1 x 1

 public class Itteration {
     public static void main( String[] args ){
         int x = 5;
         int factorial = 1;
         for ( int i = x; i > 0; i-- ){
             factorial = factorial * i;
         }
         System.out.println("!"+x+" = "+factorial);
     }   
 } 
        

Would you beleive me if i told you you could solve that problem without using a loop? look at the next example

public class Recursion {
  public static void main(String[] args){
    int x = 5;
    System.out.println("!"+x+" = "+factorial(x));
  }
  
  public static int factorial(int fact){
    if ( fact <= 0 ){
      return 1;
    } else {
      return fact * factorial(fact - 1);
    }
  
  }
}	
Home Work

1. Write a public static method that takes a string called name and prints

 My name is ""name"" 

2. Write a public static method that takes two integers and returns true if they are equal. Test both equal numbers and inequal numbers in the main method