Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.

Similar presentations


Presentation on theme: "Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method."— Presentation transcript:

1 Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method overloading – recursive methods – etc.

2 Method Calls a method is called within a statement of a caller method a method belongs to an object method call is preceded by object reference and dot. System.out.println ("Hi"); object referencemethod nameparameter list a method call without object reference means this object – i.e. the object of the caller method a method that returns a value can be called within an expression double percent = scanner.nextInt() / 100.0;

3 Method Declaration methods are declared within a class of the object method declaration has a modifier, return type, name, parameter list and body public int print (String name) {} modifiers define where the method is visible, i.e. can be used – public, private (see later), can be omitted method must declare the type at its return value – void means no value is returned parameter list is enclosed in parentheses ( and ) – it can be empty modifiermethod name parameter listreturn type body

4 Method Body method's body define what the method does method's body is enclosed in braces { and } consists of a sequence of statements – executed sequentially except for method calls branch into the callee method's body control statements (see later) statements may have their own blocks – blocks are enclosed in braces { and } ; variables declared in the body are local – they are valid only within the block where they are declared – once the method returns, any local variable ceases to exist – its value is lost unless it is returned or otherwise referenced a method often assigns values to instance variables – such values are permanent

5 Actual Parameters parameters deliver additional information to a method parameters in the method call are the actual parameters actual parameters form a list – comma-separated – enclosed in parentheses ( and ) – each actual parameters is an expression during method call – the expression of each actual parameter is evaluated e.g. if int i = 2; then call to substring(i-1,i+4) results in calling substring(1,6) – the values are passed to the callee actual parameters and parameters in declaration of called method must correspond

6 Formal Parameters parameters in the method declaration are the formal parameters formal parameters form a list – comma-separated – enclosed in parentheses ( and ) – each formal parameter is a variable declaration must be declared with is type and name e.g. String substring (int from, int to) except no initialization actual parameters method must correspond to formal ones – same number (Java 5.0 relaxes this with "varargs") – in the order and type of each parameter In method's body, formal parameters are just local variables – they are valid only within the method's body – once the method returns, any of its formal parameter ceases to exist – its value is lost unless it is returned or otherwise referenced

7 Formal and Actual Parameters names of formal and actual parameters – can be different (mostly) – but can be the same, e.g. to express that they serve the same purpose when method is called, the formal parameter will be passed, i.e. assigned the value of the actual parameter – e.g. when substring (2, 5); is called, the parameter from will be assigned 2 and to will be assigned 5 – default type conversions are performed Note that objects are passed by reference – i.e. the formal parameter holds the address of the object – therefore, all changes to the object's properties are permanent

8 Formal Parameters and Instance Variables a formal parameter can have the same name as an instance variable – e.g. class Course { String name; public void setName(String name) {} } within the method, the name is that at the formal parameter – the parameter shadows the instance variable – the narrower scope decides which program element is meant to remove ambiguity, we can give the parameter another name – e.g. public void setName(String name1) {name = name1;} better: we keep the same name and prefix the instance variable with this and dot. – e.g. public void setName(String name) {this.name = name;}

9 Method Overloading methods can have the same name – provided their formal parameter lists are different, e.g. class String { public String substring (int from, int to) {} public String substring (int from) {} } such methods are overloaded – they are different methods actual parameter list determines the method it's not enough the make the return type different – syntax error

10 Recursive Methods a methods that calls itself is recursive – e.g. public String factorial (int n) {} //... factorial (n – 1); } recursion can be indirect – e.g. f calls g and g calls f recursive methods must have a case when they terminate stack overflow error results if a recursive method doesn't terminate – each call of a method needs memory space on the stack – endless calls exhaust the memory

11 Methods' Return Types and Values a methods can return one value (expression) – the type of the returned value must precede method's name in declaration – the body must have return statement with expression, e.g. public int square (int n) {} return n * n; } return can be used in a void method – to terminate it (at an unusual spot) calls of methods returns values can be used in an expression – e.g. int sumSquares = square (2) + square (3); return type and expression where method is called must match – assignment-compatible – possible automatic type conversion

12 Returning Several Values what it we want to return two (or more) values? – complicated: Java cannot do it directly! using parameters – pass as parameters objects that can be filled with return values – fill them within the method – when method terminates, actual parameters hold the values – e.g. public void unity (Point point) { point.x = 1; point.y = 1; } – … unity (at); // at now holds value (1,1) using return object – create class representing all values – fill an object with the values and return it – e.g. class Results { int n; int m; } public Results init () { Results results = new Results (); results.n = 40; results.m = 50; return results; }

13 Matters of Style never repeat the same (or similar) sequence of statements make a method and call it repeatedly – e.g. instead of: System.out.print ("number 1>"); int number1 = scanner.nextInt (); System.out.print ("number 2>"); int number2 = scanner.nextInt (); – better: int number1 = promptInt ("number 1"); int number2 = promptInt ("number 2"); – and private int promptInt (String prompt) { System.out.print (prompt + ">"); return scanner.nextInt (); } use parameters to account for differences or even use a class if it's – logically behavior of an object – general behavior

14 Program Control © 2006 William Albritton, modified J.S. method2() { method2(); method1(); new Program(); constructor other methods } method1() { } } Program() {


Download ppt "Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method."

Similar presentations


Ads by Google