Professional Documents
Culture Documents
• In most cases, you'll use the private access modifier so the method can
only be called from within the class where it's coded.
• If you need to call the method from another class, however, you can
use the public access modifier.
Continue…
• After the access modifier, you code the return type for the method,
which identifies the type of data that the method returns.
• If the method doesn't return any data, you code the void keyword as
the return type. Then, you don't code a return statement.
• After the return type, you code the name of the method. In most cases,
you '11 give the 1nethod a name that indicates the action it performs.
Continue…
• After the method name, you code a set of parentheses.
• Within the parentheses, you declare the parameters that are required by
the method.
• This is known as the parameter list.
• Later, when you call the method, you pass arguments that correspond
to these parameters.
Continue…
Continue…
Continue…
Continue…
• To allow other classes to access a method, use the public access
modifier.
• To prevent other classes fro1n accessing a method, use the private
modifier.
• To code a method that doesn't return data, use the void keyword for the
return type.
• To code a method that returns data, code a return type in the method
declaration and code a return statement in the body of the method.
Continue…
• The return statement ends the execution of the current 1nethod and
returns the specified value to the calling method.
• When you code a parameter, you must code a data type and you must
provide a name for the parameter.
How to call methods
• To start, you can type the optional this keyword to specify that the
method that's being called is in the current class, followed by the dot
operator and the method name.
• Otherwise, you just code the method name and the current class is
assumed.
• If the method requires arguments, you code the argument list within
parentheses right after the method name.
Continue…
• Otherwise, you code an empty set of parentheses.
•
• The term parameter refers to the variables of a method declaration, and
the term arguments refers to the values that are passed to a method.
• When you call a method, you typically code the arguments in the same
sequence that's used by the para1neter list in the method.
• In fact, it's a good coding practice to pass arguments that have the
same data types as the parameters in the method.
Continue…
Continue…
Continue…
• When you call a method, you code an argument list with the
arguments that are passed to the method. You can pass arguments by
position or by name.
• The arguments you pass to a method must have data types that are
compatible with their corresponding parameters.
How to use optional parameters
• When you code a method, you 1nay occasionally want to provide
default values for one or more parameters in case the user doesn't
provide values for them.
• When you use optional parameters, you may also want to pass
arguments by name.
• You can also combine the two techniques for passing arguments and
pass some by position and others by name.