They use the information stored in variables to complete a task.
Variables and methods live together inside of Classes.
A method is the same thing as a function or subroutine.
First, write a Class:
public class CreateMethod
{
}// end of CreateMethod
Then, add the main( ) method so you can run this Class:
public class CreateMethod
{
public static void main(String[ ] args)
{
}
}// end of
Tip: Save this code (CTRL S) and compile it (CTRL 1) to make certain you don't have any typos.
Public methods can be used by any other Class. Private methods can only be used by other methods in the same Class. Most methods are public and if you aren't sure, make it public.
Start a new method under the main( ) by adding the keyword "public":
public class CreateMethod
{
public static void main(String[ ] args)
{
}
public
}// end of CreateMethod
Methods often have results that they want to send back to whatever called them
in the first place. Java needs to know what type of object will be sent back. Commonly
one of the 8 primitive datatypes is used, but Classes/objects (such as String)
can be designated as well. Notice that a method can only return a single object.
If nothing is going to be returned then the keyword "void" must used.
Tell Java that no objects will be returned by this particular method by using the keyword "void":
public class CreateMethod
{
public static void main(String[ ] args)
{
}
public void
}// end of CreateMethod
You can name a method anything you want as long as it isn't the same as a Java keyword. By convention all methods start with a lowercase letter. If there are multiple words, then capitalize the first letter of each word. It is also helpful to always include a set of parenthesis ( ) to designate that it is a method.
Let's call our method printNameAge( ) by adding the name, the parenthesis, and a set of curly brackets containing a single blank line:
public class CreateMethod
{
public static void main(String[ ] args)
{
}
public void printNameAge( )
{
}
}// end of CreateMethod
*Note: All of the methods that are in the same class as a main(
) method must be declared static. In these cases the signature line for the method
should include the keyword "static":
public static void
printNameAge( )
{
}// end of CreateMethod
Most methods are stored in other Classes and shouldn't use this keyword. The reason is that you will want to make many instances or copies of the methods. The keyword "static" tells Java that only one copy is allowed in memory. This is how Java makes certain that there is only one main( ) in a program.
Java was written so every method can be sent secret messages or parameters. But, Java needs to know what type of object each parameter will be. The type as well as a variable name that will be used inside the method is declared inside the parenthesis for each parameter. If there are more than one parameters, these declarations are separated using commas.
For example, let's say there is a method called printFullName( ) that displays a first name and a last name. These can be passed in as parameters like this:
public void printFullName(String fname, String lname)
{}
Notice that these "secret messages" or parameters are simply variable declarations
separated by commas.
If there are no parameters to pass, simply leave the ( ) empty.
Our method is expecting two parameters, a String containing the user's name and an int containing the user's age:
public class CreateMethod
{
public static void main(String[ ] args)
{
}
public static void printNameAge(String
name,
int age)
{
}
}// end of CreateMethod
You now have the complete signature line of the method. Notice each part is always written in the same order:
(1) public/private (2) type of returning object (3)name of method (4) (parameters declared as variables)
All that is left is to write the code that you want the method to do.
Let's output the user's name and age:
public class CreateMethod
{
public static void main(String[ ] args)
{
}
public static void printNameAge(String
name, int age)
{
System.out.println("Hello
" + name);
System.out.println("You are
" + age + " years old.");
}
}// end of CreateMethod
Methods call other methods. Let's go back to our main( ) method and call our new method. We can either pass in actual values or we can use variables that contain the values.
Use actual values first:
public class CreateMethod
{
public static void main(String[ ] args)
{
printNameAge("Abe
Lincoln", 194);
}
public static void printNameAge(String
name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
}// end of CreateMethod
(1) When you run the CreateMethod class, Java looks for the main( ) method.
(2) Inside the main( ) method it finds the call to printNameAge( ) and jumps to
that
code sending along a copy of "Abe Lincoln" and 194.
(3) As Java enters printNameAge( ) the two variables "name" and "age" are initializedwith
the values of "Abe Lincoln" and 194. (The order is important because of this
initialization.)
(4) Java runs the procedural statements inside printNameAge( ) which outputs the
two lines
(5) There is nothing to return (because of the "void" keyword in the signature line)
so Java simply returns to main( ) after the two lines are output.
(6) Back in main( ) there's nothing left to do, so Java quits main( ) and ends the
class.
(7) Ta Daaa!
Methods can also be called using variables.
Inside the main( ) method declare and initialize two variables, presidentName
and ageInYears.
Then call the method using the variable names instead of the actual values
:
public class CreateMethod
{
public static void main(String[ ] args)
{
// DATA LAYER
String presidentName = "John F. Kennedy";
int age = 86;
// INTERFACE LAYER (output)
// this mixes data in with interface
:-(
printNameAge("Abe
Lincoln", 194);
// using variables allows separating
data and interface :-)
printNameAge(presidentName,
age);
}
public static void printNameAge(String
name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
}// end of CreateMethod
Notice that the order you list the parameters is important!
Also, keep in mind that only copies of the variables are passed to the method printNameAge(
). These copies are used to initialize new variables that were declared inside
the parenthesis.
You are a smart programmer if you name the two sets of variables different. For
example: the variable "presidentName" is copied into the variable "name".
(Keep your variable names singular, without an 's'. It will simplify your programming!)
Finally, notice the reuse of code. One method can be called many, many times.
This is one of the main advantages of having methods.