Using Java Methods

Using Java Methods

Previous Home Next

 

As discussed previously, methods are named blocks of code that can be called from elsewhere in your program in order to accomplish some task.  

  More generally, method declarations have six components, in order:

   1. Modifiers—such as public, private, and others you will learn about later.
   2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
   3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
   4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
   5. An exception list—to be discussed later.
   6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

 


 
/ overloading methods
public class DataArtist
{
...
public void draw(String s)
{
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...


Previous Home Next