Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Methods: A Deeper Look

Similar presentations


Presentation on theme: "Chapter 6 Methods: A Deeper Look"— Presentation transcript:

1 Chapter 6 Methods: A Deeper Look
T.A. Norah Alshareef © Copyright by Pearson Education, Inc. All Rights Reserved.

2 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

3 6.1  Introduction Best way to develop and maintain a large program is to construct it from small, simple pieces, or modules. Topics in this chapter static methods Declare a method with more than one parameter Method-call stack Method overloading. © Copyright by Pearson Education, Inc. All Rights Reserved.

4 6.2 Program Modules in Java
Java programs combine new methods and classes (that you write) with predefined methods and classes (available in the Java Application Programming Interface and in other class libraries. ) Related classes are typically grouped into packages, so that they can be imported into programs and reused. © Copyright by Pearson Education, Inc. All Rights Reserved.

5 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.

6 6.2 Program Modules in Java (Cont.)
Methods help you modularize a program by separating its tasks into self-contained units. Statements in method bodies Written only once Hidden from other methods Can be reused from several locations in a program Software reusability Use existing methods as building blocks to create new programs. Dividing a program into meaningful methods makes the program easier to debug and maintain. © Copyright by Pearson Education, Inc. All Rights Reserved.

7 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

8

9 6.2 Program Modules in Java (Cont.)
Hierarchical form of management (Fig. 6.1). A boss (the caller) asks a worker (the called method) to perform a task and report back (return) the results after completing the task. The boss method does not know how the worker method performs its designated tasks. The worker may also call other worker methods, unbeknown to the boss. © Copyright by Pearson Education, Inc. All Rights Reserved.

10 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

11 6.3 static Methods, static Fields
Sometimes a method performs a task that does not depend on the contents of any object. Applies to the class in which it’s declared as a whole Known as a ” static method” or a class method It’s common for classes to contain convenient static methods to perform common tasks. © Copyright by Pearson Education, Inc. All Rights Reserved.

12 6.3 static Methods, static Fields and Class Math
To declare a method as static, place the keyword static before the return type in the method’s declaration. Calling a static method ClassName.methodName( arguments ) if called outside the class Or only methodName( arguments ) if called within the same class Method arguments generally may be constants, variables or expressions. Class Math provides a collection of static methods that enable you to perform common mathematical calculations. © Copyright by Pearson Education, Inc. All Rights Reserved.

13 Where I can find the class Math ?!
The Java.lang package contains also the Class String and the Class System .. © Copyright by Pearson Education, Inc. All Rights Reserved.

14 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

15 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

16 6.3 static Methods, static Fields and Class Math (Cont.)
Math fields for common mathematical constants Math.PI ( ) Math.E ( ) Those fields declared in class Math with the modifiers: public, final and static public allows  you to use these fields in your own classes. Final is constant —its value cannot change after the field is initialized and this why PI and E are written in Capitals Static  Can be used before creating any objects( instances) and all objects ( instances) of the class share one copy of those fields. PI and E are declared final because their values never change. © Copyright by Pearson Education, Inc. All Rights Reserved.

17 6.3 static Methods, static Fields and (Cont.)
A field (that represents an attribute) is also known as an instance variable each object (instance) of the class has a separate instance (copy) of the variable in memory. Fields for which each object of a class does not have a separate instance (copy) of them are  declared static and are also known as class variables. All objects of a class containing static fields  share one copy of those fields. Together the class variables (i.e., static variables) and instance variables represent the fields of a class. © Copyright by Pearson Education, Inc. All Rights Reserved.

18 6.3 static Methods, static Fields and (Cont.)
Simple example to see how Static Fields can be declared and used : We've declared a class called 'Stuff' and given it one public static variable of type String. We've initialized the variable to the String value "I'm a static variable". © Copyright by Pearson Education, Inc. All Rights Reserved.

19 6.3 static Methods, static Fields and (Cont.)
Simple example to see how Static Fields can be declared and used (cont..) Now we can use this class in our main program like this: Application.java: Output : I’m a static variable

20 6.3 static Methods, static Fields and (Cont.)
Why is method main declared static? The JVM attempts to invoke the main method of the class you specify—when no objects of the class have been created. Declaring main as static allows the JVM to invoke main without creating an instance of the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

21 6.4 Declaring Methods with Multiple Parameters
Multiple parameters are specified as a comma-separated list. There must be one argument in the method call for each parameter (sometimes called a formal parameter) in the method declaration. Each argument must be consistent with the type of the corresponding parameter. © Copyright by Pearson Education, Inc. All Rights Reserved.

22

23

24

25 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

26 How you can decide if a variable should be declared as a field or a local variable?

27 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

28 6.4 Declaring Methods with Multiple Parameters (Cont.)
Implementing method maximum by reusing method Math.max : Two calls to Math.max, as follows: return Math.max( x, Math.max( y, z ) ); The first specifies arguments x and Math.max( y, z ). Before any method can be called, its arguments must be evaluated to determine their values. If an argument is a method call, the method call must be performed to determine its return value. The result of the first call is passed as the second argument to the other call, which returns the larger of its two arguments. © Copyright by Pearson Education, Inc. All Rights Reserved.

29 6.5 Notes on Declaring and Using Methods
Three ways to call a method: Using a method name by itself  to call another method of the same class such as maximum (number 1 number 2, number3) Using a variable that contains a reference to an object, Object name. method name  to call a method of the referenced object Using the class name and a dot (.) followed by class name  to call a static method of a class © Copyright by Pearson Education, Inc. All Rights Reserved.

30 6.5 Notes on Declaring and Using Methods (Cont.)
A non-static method can call any method of the same class directly and can manipulate any of the class’s fields directly. A static method can call only other static methods of the same class directly can manipulate only static fields in the same class directly. To access the class’s non-static members, a static method must use a reference to an object of the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

31 6.5 Notes on Declaring and Using Methods (Cont.)
Three ways to return control to the statement that calls a method: When the program flow reaches the method-ending right brace } When the following statement executes return; When the method returns a result with a statement like return expression; © Copyright by Pearson Education, Inc. All Rights Reserved.

32 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

33

34

35

36 JAVA API packages examples ..

37 JAVA API packages examples ..

38 JAVA API packages examples ..

39 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

40 Method’s Signature Method-name (formal-parameter-types)
Does not include the return-type Within any class definition, the signature must be unique The same method-name with different formal parameters types can be used to implement overloading of methods.

41 6.12 Method Overloading Method overloading
Methods of the same name declared in the same class Must have different sets of parameters Compiler selects the appropriate method to call by examining : the number, types and order of the arguments in the call. Used to  create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments. © Copyright by Pearson Education, Inc. All Rights Reserved.

42 6.12 Method Overloading (cont.)
Distinguishing Between Overloaded Methods: The compiler distinguishes overloaded methods by their signatures: (the methods‘names and the number, types and order of their parameters. ) Return types of overloaded methods Method calls cannot be distinguished by return type. Overloaded methods can have different return types if the methods have different parameter lists. Overloaded methods need not have the same number of parameters. © Copyright by Pearson Education, Inc. All Rights Reserved.

43

44 Examples Related to this Lecture
© Copyright by Pearson Education, Inc. All Rights Reserved.

45 Example 1: Overloading – Different Number of parameters in argument list
When methods name are same but number of arguments are different.

46 Example 1: Overloading – Different Number of parameters in argument list

47 Example 2: Overloading – Difference in data type of arguments

48 Example 2: Overloading – Difference in data type of arguments
In the above example, method disp() is overloaded based on the data type of arguments – Like example 1 here also, we have two definition of method disp(), one with char argument and another with int argument.

49 Example3: Overloading – Sequence of data type of arguments

50 Example3: Overloading – Sequence of data type of arguments
Here method disp() is overloaded based on sequence of data type of arguments – Both the methods have different sequence of data type in argument list. First method is having argument list as (char, int) and second is having (int, char). Since the sequence is different, the method can be overloaded without any issues.

51 few Valid / invalid cases of method overloading

52

53

54

55

56

57 Questions

58 Question 1 – Find out if the following program will throw compilation error or not , if there is a error explain why?

59 Question 1 – Find out if the following program will throw compilation error or not , if there is a error explain why? Answer:

60 Question 2 – Find out if the following program will throw compilation error or not , if there is a error explain why?

61 Question 2 – Find out if the following program will throw compilation error or not , if there is a error explain why? Answer:

62 End © Copyright by Pearson Education, Inc. All Rights Reserved.


Download ppt "Chapter 6 Methods: A Deeper Look"

Similar presentations


Ads by Google