[ [[tutorial-en-001 | Intro]] | [[tutorial-en-002 | Begin]] | [[tutorial-en-003 | if/else]] | [[tutorial-en-004 | Loops]] | [[tutorial-en-005 | Arrays]] | [[tutorial-en-006 | Graphics]] | [[tutorial-en-007 | Animation]] | [[tutorial-en-008 | Mouse]] | [[tutorial-en-009 | Game]] | [[tutorial-en-010 | Real]] | [[tutorial-en-011 | Methods]] | [[tutorial-en-012 | Class]] | [[tutorial-en-013 | Class 2]] | [[tutorial-en-014 | Applet]] | [[tutorial-en-015 | MouseClick]] | [[tutorial-en-016 | Thread]] | [[tutorial-en-017 | Button]] ] ---- ====== Java Methods ====== A java method is a series of statements that perform some repeated task. Instead of writing 10 lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut. For example if we had to repeatedly output a header such as: System.out.println("Feral Production"); System.out.println("For all your Forest Videos"); System.out.println("427 Blackbutt Way"); System.out.println("Chaelundi Forest"); System.out.println("NSW 2473"); System.out.println("Australia"); We could put it all in a method like this: public static void printHeader() { System.out.println("Feral Production"); System.out.println("For all your Forest Videos"); System.out.println("427 Blackbutt Way"); System.out.println("Chaelundi Forest"); System.out.println("NSW 2473"); System.out.println("Australia"); } And to call it we simply write: printHeader(); And it will print out: Feral Productions For all your Forest Videos 427 Blackbutt Way Chaelundi Forest NSW 2473 Australia So lets write it in a java program: public class Invoice { public static void main(String[] args) { // call our method in the main method printHeader(); System.out.println("You owe us $47.00"); } // declare the method outside the main method // but inside the end curly bracket for the class public static void printHeader() { System.out.println("Feral Production"); System.out.println("For all your Forest Videos"); System.out.println("427 Blackbutt Way"); System.out.println("Chaelundi Forest"); System.out.println("NSW 2473"); System.out.println("Australia"); } } Compile that (javac Invoice.java) and then run it (java Invoice). What did you get? You should have printed out the header and then the line "U owe us... blah blah" ===== Java Method Return Types ===== Notice that our first method had void in its declaration. Void means that nothing is returned. A series of tasks or calculation may be performed but nothing is equal to our method. I shall explain what the last line means soon. If we wanted something returned we would have to declare it in our declaration for the method: public int getXposition() will return an integer value. Here is an example of this: public class Area { public static void main(String[] args) { int length = 10; int width = 5; // calling the method or implementing it int theArea = calculateArea(); System.out.println(theArea); } // our declaration of the method public static int calculateArea() { int methodArea = length * width; return methodArea; } } What was our output? should have been 50. The method calculateArea() is equal to 50! We can put the result of a methods processing directly into a variable or use it directly as in: System.out.println("The area is " + calculateArea()); or: System.out.println("The area is " + theArea); Exactly the same thing. Same horse, different jockey. Any type of datatype can be returned. We can have a double, a boolean, a String. What else? Some examples: * public int getXposition() * public double calculateWage() * public String getMyName() * public boolean isFinished() * public void paintEnemy() * public char getMyInitial() ===== Java Methods passing arguments ===== Methods can receive input through the use of arguments. They are use like this: public class Enemy { int posX; int posY; public void setXposition(int _x) { posX = _x; } } This method will accept or input an int as specified in the declaration (int _x). Arguments can be of any java datatype. They can be double, int, String, char, boolean, etc etc. The method above allows a manager class to set the xposition of the Enemy class, probably moving it. It could be called in the Manager class like so: Enemy yukky = new Enemy(); yukky.setXposition(150); This would place the yukky Enemy at the x position of 150. Another example: public void calculateTax(double grossWage) { taxation = grossWage * 0.56; } You would have the variable double taxation declared in the body of the class and then you pass the grossWage to it and it will calculate the taxation owing (yuk!). You could call it like so: double taxation; calculateTax(350.00); System.out.println(taxation); What would be the output of that couple of lines of code? A datatype passed as an argument must be of the same type as that in the declaration of the method. If a method is declared like so: public void setValue(int amount); you cant use it like so: setValue(35.50); // no no this is passing it a double.. wrong, wrong!! or: setValue("Star Wars Sux"); // no no no. Star Wars is great! Try to pass the wrong type of data to a method. See the error when you compile it. Dont believe me! Test it for yourself. Also, passed arguments must be called in the order that they are declared: // declare our method public void setAmounts(int _num1, String _name) { classesNum1 = _num1; classesName = _name; } This setter methods takes an int first and then a String. You have to pass the arguments in the correct order. You MUST call the method like this: setAmounts(27,"Steve"); You will get an error if you try this: setAmounts("steve", 27); You can have as many arguments as you like but they all still have to be in the correct order. You also can have a return value for methods that pass arguments. For example: public int calculateArea(int _width, int _height) { int myArea = _width * _height; return myArea; } So if you called it like this: houseArea = calculateArea(20, 30); houseArea would have a value of 600. [[tutorial-en-012 | Next lesson]] we will go on to learn about classes and how to use them.