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");We could put it all in a method like this:
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");
public static void printHeader(){And to call it we simply write:
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");
}
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{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"
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");
}
}
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{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:
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;
}
}
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{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.
int posX;
int posY;
public void setXposition(int _x){
posX = _x;
}
}
It could be called in the Manager class like so:
Enemy yukky = new Enemy();This would place the yukky Enemy at the x position of 150. Another example:
yukky.setXposition(150);
public void calculateTax(double grossWage){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:
taxation = grossWage * 0.56;
}
double 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 :
calculateTax(350.00);
System.out.println(taxation);
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 methodThis 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");
public void setAmounts(int _num1 , String _name){
classesNum1 = _num1;
classesName = _name;
}
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){So if you called it like this :
int myArea = _width * _height;
return myArea;
}
houseArea = calculateArea(20, 30);
houseArea would have a value of 600.