Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature,...

23
Java Methods By J. W. Rider

Transcript of Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature,...

Page 1: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Java Methods

By J. W. Rider

Page 2: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Java Methods

ModularityDeclaring methods– Header, signature, prototype

StaticVoidLocal variables– this

ReturnReentrancyMethod-like structures– Constructors, static initializers

Page 3: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 4: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 5: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

General method declarations

Headerqualifiers return-type method-name

( formal-parameters ) throws-clause

Body{

Statements;

}

Page 6: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Application main() example

public static void main(String[] args)

{

System.out.println(“Hello, world!”);

}

Page 7: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Signature

Method-name (formal-parameter-types)Does not include the return-typeWithin any class definition, the signature must be unique

Page 8: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Prototype

Return-type method-name (formal-parameter-types)The prototype defines the kinds of messages to which the class or its instances respond.

Page 9: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Qualifiers

Scope (external visibility)– public, protected, private

static

Miscellaneous– final, abstract, native

Page 10: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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

Page 11: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 12: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 13: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 14: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 15: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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

Page 16: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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 bodyEven if no arguments are to be passed to a method, empty parentheses are used to distinguish between a field and method.

Page 17: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Method references

Method declarations represent potential behavior. This behavior does not occur until the method is referenced elsewhere.

methodname( actual-arguments )

Page 18: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 19: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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

Page 20: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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.

Page 21: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Related structures

Two other places where executable statements may be located:– Constructors– Static initializers

Page 22: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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

Page 23: Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

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 staticMultiple static initializers may be declared within a class