Methods
~ When using a method, are said to invoke or call it.
~ Java has two kinds of methods :
+ Methods that return a single item. Example: method nextInt
+ Methods that perform some action other than returning an item.
Example: method println
~ Method that perform some action other than returning a value are
called void methods.
~ A method defined in a class is usually invoked using an object of that class.
Defining void Methods
~ A method definition consists of a heading (1st part of the method
definition) and a body (the rest of the method definition).
~ The statements in the body are enclosed between braces {}.
~ Instance variables can be use within the body. Such variables are
called local variables.
*Local variable is confined to the method containing its declaration.
Blocks
~ Is a compound statement that declares a local variable.
~ Same as a compound statement that is a group of
Java statements enclosed in braces {}.
~ However, the 2 terms tend to be used in different contexts.
~ The variables declared in a block are local to the block and these
variables disappear when the execution of the block ends.
Parameters of a Primitive Type
~ A formal parameter in a method definition represents the argument.
~ It is given in the heading, within the parentheses right after the
method name.
~ A formal parameter of a primitive type, such as int, double or
char is a local variable.
~ Example: public int predictPopulation (int years)
+ The word years is a formal parameter.
+ It is a stand-in value that will plugged in when thr method is called.
+ The item that is plugged in is called an argument or in some
other books called actual parameters.
~ The way of plugging in arguments (the value of the argument) for
formal parameters is known as the call-by-value mechanism.
~ This is the only mechanism used for parameters of a primitive
type in Java.
~ The data type of a formal parameter in a method heading is
written before the parameter.
Void methods with parameters
modifier (s) void methodName (formal parameter list)
{
statements
}
Methods Calling Methods
~ A method body can contain an invocation of another method.
~ If the called method is in the same class, it is typically invoked
without writing any receiving object.
~ Java has two kinds of methods :
+ Methods that return a single item. Example: method nextInt
+ Methods that perform some action other than returning an item.
Example: method println
~ Method that perform some action other than returning a value are
called void methods.
~ A method defined in a class is usually invoked using an object of that class.
Defining void Methods
~ A method definition consists of a heading (1st part of the method
definition) and a body (the rest of the method definition).
~ Normally, the heading is written on a single line, but if it is too
long for 1 line, it can be broken into 2 or more lines.~ The statements in the body are enclosed between braces {}.
~ Instance variables can be use within the body. Such variables are
called local variables.
*Local variable is confined to the method containing its declaration.
Blocks
~ Is a compound statement that declares a local variable.
~ Same as a compound statement that is a group of
Java statements enclosed in braces {}.
~ However, the 2 terms tend to be used in different contexts.
~ The variables declared in a block are local to the block and these
variables disappear when the execution of the block ends.
Parameters of a Primitive Type
~ A formal parameter in a method definition represents the argument.
~ It is given in the heading, within the parentheses right after the
method name.
~ A formal parameter of a primitive type, such as int, double or
char is a local variable.
~ Example: public int predictPopulation (int years)
+ The word years is a formal parameter.
+ It is a stand-in value that will plugged in when thr method is called.
+ The item that is plugged in is called an argument or in some
other books called actual parameters.
~ The way of plugging in arguments (the value of the argument) for
formal parameters is known as the call-by-value mechanism.
~ This is the only mechanism used for parameters of a primitive
type in Java.
~ The data type of a formal parameter in a method heading is
written before the parameter.
Void methods with parameters
modifier (s) void methodName (formal parameter list)
{
statements
}
Methods Calling Methods
~ A method body can contain an invocation of another method.
~ If the called method is in the same class, it is typically invoked
without writing any receiving object.
0 comments: