Monday, February 25, 2013

Methods and method parameters

Consider a method (also called a behaviour) for logging on to a network. When a user logs on, we could reasonably assume that the user must provide a username and password. A method for logging on could look like this:


This method signature can be broken down as follows:

public - The visibility of the method. A method that is public (as opposed to private) can be seen by other classes.

void - This indicates that the method will not return a value. Some methods return values (as in the case of the Scanner’s nextInt() function).

login - This is the name of method. Method names are named in the same way as variables in that they should start with a lower case letter. If the method name contains serveral words, each subsequent word starts with an uppercase letter. Method names must be unique - except where method overloading is used.

(String userName, String password) - This is the parameter list for method. When a method requires some data to perform its task, a list of that data is specified. This parameter list contains 2 parameters (although a parameter list may contain more, less or no parameters). 

Each parameter has a name, a type and is separated by a comma.

No comments:

Post a Comment