Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4
| Lesson 5 | Lesson 6 | Lesson 7 | Lesson 8 | Lesson 9 | Lesson 10
In this lesson we will be covering methods. What are methods? Methods are calls to actions, they perform an operation. Methods make programing easier. Here is what methods look like, if you see it, it's a method. nameOrWhatever() { Just look for the set of parenteses. Sometimes there are parameter or arguments inside the () (i.e.) void cooking (int hours) { int hours would be the parameter
class methodS {
public static void main(String[] args) {
numbers();
}
static void numbers() {
System.out.println("three");
}
}
|
Look at:
static void numbers() {
System.out.println("three");
}
that would be defining a method. We are setting the method up. But nothing is actually done without the numbers(); statement. The numbers(); statement calls the method. When it is called, it executes what it is defined as. Now if you ran the program above it should of printed out "three". Let's look at the code in detail, static I will get to later but for now we put it there because the main method is also static. void means it does not return a value, meaning its usesless, we get nothing back, no String, int, boolean, ect. Only what is writen in the method block is outputed.
The name of a method always comes before the set of parentheses (). To make simple of void numbers(), we have a method named numbers, the method returns nothing, and has no parameters. (Nothing in the parentheses)
Let's do an exercise. Type in this.
class methodS {
public static void main(String[] args) {
numbers();
numbers();
numbers();
}
static void numbers() {
System.out.println("three");
}
}
|
Methods could be used to save typing. You should get three "three"s printed out. Now try it without any numbers();. What did you get? Why? That's because we did not call the method, we only defined it.
Now let's try to make a method that returns a value. So we will omit the void. In this case we will make it return an int value. (It could be anything, String, boolean, ect.)
Type this in and run:
class methodS {
public static void main(String[] args) {
numbers();
numbers();
numbers();
}
static int numbers() {
System.out.println("three");
return 3;
}
}
|
Well why doesn't the number 3 appear, but only the word three? This is because it returns a value of an int (3) but has no place to store it, therefore it is just thrown away. So to make it useful we need to store it some where.
class methodS {
public static void main(String[] args) {
int returnValue = numbers();
System.out.printline(returnValue);
}
static int numbers() {
System.out.println("three");
return 3;
}
}
|
Now let's try adding some parameters. That would be the stuff inside of the parentheses.
class methodS {
public static void main(String[] args) {
printNumber(5);
}
static void printNumber(int n) {
System.out.println(n);
}
}
|
So we have int n as an argument. We have a method named printNumber and it does not return anything. The argument means we have to have an int when we call the method.(the 5)
What it says is, printNumber(5) prints the number 5. Look at the printNumber method. Although we haven't created a variable n, we can write System.out.println(n);. When the method is called, a variable of type int called n is created, which contains the value 5. If we call the method again, we can put another value in n:
class methodS {
public static void main(String[] args) {
printNumber(5);
printNumber(4);
}
static void printNumber(int n) {
System.out.println(n);
}
}
|
Note:String, Booleans, Doubles, etc. can be use not just ints.
Summary:
By the end of this lesson you should have learned about methods, how they are used, how to call/define them, method terms, and functions in methods (i.e.) returns/no returns.
|
Copyright ©2004 Java Wave, All rights reserve.
|