Method Example
The Code
The code for the seventh class of Java is as follows:
public class MethodExample {
// @param args the command line arguments
public static void main(String[] args) {
// Rudimentary program to show how methods work
Howya();
Adder(10,69);
System.out.println("Again!");
Adder(14,21);
Howya();
int h = AdderWithOutput(36,64,39);
System.out.println("The output is " +h);
}// End of main
/*
While methods can be within main(), it is best to place them
here, between the }}'s for the end of main() and the end of
the class.
*/
/*
Simple method to just print out simple statement.
Note the keywords.
1. public - Can be accessed by all
2. static - It is a method that does not need an associated object
to be created
3. void - Nothing is returned from the method
*/
public static void Howya() {
System.out.println("Hello - How are you");
}
// Method that takes in two inputs but does not return anything
public static void Adder(int x, int y) {
int z = x + y;
System.out.println("The answer is " +z);
}
// Method that takes three inputs and returns one output
public static int AdderWithOutput(int a, int b, int c) {
int e = a+b+c;
return e; // return the variable e to the main program
}
}// End of class