Java: Class Methods
protection_type return_type method_name ( var1, var2, ...)
{ .... }
Methods can be declared with a protection type or status
that restricts who can access them. See the page on
Protection Types for more information.
Local Variables
Any variable defined inside of a method has a scope local to that
method.
Return Type
A method can return nothing (void) or can retrun any kind of class,
variable or an array.
Passing Arguments
The variables are passed by reference
- Thus changes are retained after the method is done executing.
Static Methods
Static methods can accessed without creating an instance of
this class.
Every static method is implicitly final (see below) and hence
can not be overwritten. There is a Math class in Java and
a static method called sqrt() that can be called as follows:
float value = Math.sqrt(200.0);
Note that an instance of the class Math is not needed to invoke
the static method sqrt.
Also, if a method is declared static it means that it cannot access any
instance variables of the class that are not also declared
static. For example,
class Document {
static int version=10;
int Num_Chapters;
static void modify_version(int ver) {
version = ver;
/*can not access Num_Chapters*/
}
void update_info(int ver, int num) {
version = ver;
Num_Chapters = num;
}
}
Final Methods
If a method is declared as final then it can not be overridden.