Java Methods - J. W. Rider
Download
Report
Transcript Java Methods - J. W. Rider
Java Methods
By J. W. Rider
Java Methods
Modularity
Declaring methods
– Header, signature, prototype
Static
Void
Local variables
– this
Return
Reentrancy
Method-like structures
– Constructors, static initializers
Modularity in Java
Packages are composed of one or more classes
or interfaces.
Classes and interfaces are composed of one
more members; either data members (fields) or
function members (methods).
Methods are composed of statements; either
declarations (local variables) or executable
statements.
Object-oriented methods
Methods define the kinds of messages (and
parameters) which objects of a particular class
may receive.
All object-oriented behavior is implemented
through methods.
General method declarations
Header
qualifiers return-type method-name
( formal-parameters ) throwsclause
Body
{
Statements;
}
Application main() example
public static void main(String[] args)
{
System.out.println(“Hello, world!”);
}
Signature
Method-name (formal-parameter-types)
Does not include the return-type
Within any class definition, the signature must be
unique
Prototype
Return-type method-name (formal-parametertypes)
The prototype defines the kinds of messages to
which the class or its instances respond.
Qualifiers
Scope (external visibility)
– public, protected, private
static
Miscellaneous
– final, abstract, native
The static qualifier
The static qualifier divides class members into
two groups:
– Those members which are shared by all instances of
the class and exist whether or not any instances
exist.
– Those members which exist in individual objects and
are independent from the same named members in
other instances of the same class
Static constraints
A static field defines a single variable shared by all
instances of the class.
A non-static field defines a variable that is replicated
whenever a new instances of the class is constructed.
A static method may not make an unqualified reference
to any static member (field or method).
A non-static method may make unqualified references
to both static and non-static members.
– May also use the reserved this variable.
The this variable
Within each non-static method, Java automatically
creates a reference variable named this that references
the “current” instance of the class for which the method
has been invoked.
Whenever a non-static member is referenced within a
non-static method, this is understood to be the target of
the message.
Every reference in Java must be handled through either
a class (for static references) or through an object.
Return types
The return type of a method may be any type
used for a variable declaration.
– These methods may be invoked wherever an
expression is permitted in an executable statement
or declaration.
The return type of a method may also be void.
– These methods may be invoked only by an
executable statement.
The void type
May only be used as a return-type for a method.
– Do not try to use void as a variable type.
Void indicates that the method does not return a
value.
– Such methods are used for their “side-effects,” such
as output and state variable changes.
The return statement
For void methods:
– The return; statement is optional
For other methods:
– The method must terminate by a statement of the
form “return expression;”
– The expression must evaluate to the same type as
the return-type of the method
Formal parameters
Variable declarations within the header of a method:
–
–
–
–
–
Enclosed by parentheses
Separated by commas
May not be initialized (no “default” parameter values in Java)
The types of the variables are used to identify the method
The names of the variables may only be used within the
method body
Even if no arguments are to be passed to a method,
empty parentheses are used to distinguish between a
field and method.
Method references
Method declarations represent potential
behavior. This behavior does not occur until the
method is referenced elsewhere.
methodname( actual-arguments )
Actual arguments
Expressions, enclosed within parentheses and
separated by commas.
Java uses the expression types to determine
which one of several overloaded methods of the
same name should be invoked.
Java uses the expression values as
initializations for the formal parameters.
Invocation examples
Void methods
– doHello();
– object1.setInit(5);
Return-value methods
– x=object4.getInit();
– setInit(object2.getStatus()); // nesting
– object3.getBase().doHello(); // chaining
Static methods
– obj5.method(); // implicit
– MyClass.method(); // explicit
Multiple invocations
At any given point in time, a single method may
have been invoked through multiple locations in
a program.
Every invocation gets its own copy of the
methods local variables (including the formal
parameters)
A method may even call invoke itself. This is
called recursion.
Related structures
Two other places where executable statements
may be located:
– Constructors
– Static initializers
Constructors
A constructor is invoked every time an instance of an
object is created (e.g., with the new operator.)
Constructor declarations are like ordinary method
declarations with two important exceptions:
– There is no return type; not even void
– The name of the constructor is always the name of the class
Static initializers (Advanced!)
Static initializers are invoked when a class is
loaded into memory (before creating a single
instance of the class)
The header of a static initializer consists of the
reserved word static
Multiple static initializers may be declared within
a class