Methods -- Style 1
(does not receive arguments, does not return values)
Style 1: Our first style of
method will simply perform an independent
task. It will not receive any parameters and it will not return
any values. Such a method definition starts with the reserved words public static void, followed by the name of the method and
a set of parentheses. The word public indicates that there are no restrictions on the use of the method. The
word static means that all of the method's data
will come from parameters or internal computations, with no reference to
variables. The
word void is used to indicate that no value is
returned.
NOTE: When using a method, it is customary to "comment" the method in some manner. Such documentation allows readers of the program, or other programmers also working on the program, to understand the method's purpose in the program. The commenting may be extensive, including detailed information about the method using the documentation comments (/** */). In this course, we will not be adhering to the detailed documentation, but we will be listing simple, informative documentation.
//Demo Program for Methods
public class DisplayClass
{
public static void main (String[ ] args) {
System.out.println("Calling all methods!");
asterisks( ); //invoking method
(calling the method)
System.out.println("Great
job, method!");
asterisks( ); //invoking method again
System.out.println("We're outta here!");
}
//Method for asterisks
public static
void asterisks( ) { // Method definition
int count;
for(count = 1; count<=20; count++)
System.out.print("*");
System.out.println( );
}
}
Output:
Calling all methods!
********************
Great job, method!
********************
We're outta here! |
Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms
of Use | JavaMathBits.com
|