0% found this document useful (0 votes)
7 views

Java_PPT_ch03-1

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java_PPT_ch03-1

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 3:

Using Methods, Classes,


and Objects
2 Objectives
 Create methods
 Add parameters to methods
 Create methods that return values
 Study class concepts
 Create a class
 Create instance methods in a class

Java Programming, Sixth Edition


3 Objectives (cont'd.)
 Declare objects and use their methods
 Organize classes
 Create constructors
 Appreciate classes as data types

Java Programming, Sixth Edition


4 Creating Methods

 Method
 Program module
 Contains series of statements
 Carries out task
 Execute method
 Invoke or call from another method
 Calling method (client method)
 Makes method call
 Called method
 Invoked by calling method

Java Programming, Sixth Edition


5 Creating Methods (cont'd.)

 Method must include:


 Method header
 Also called declaration

 Method body
 Between a pair of curly braces
 Contains the statements that carry out the work
 Also called implementation

Java Programming, Sixth Edition


6 Creating Methods (cont'd.)

 Method declaration
 Optional access specifiers
 Return type for method
 Method name
 Set of parentheses
 Might contain data to be sent to the method

 Place entire method within class that will use it


 Not within any other method
 Fully qualified identifier
 Complete name that includes the class

Java Programming, Sixth Edition


7 Creating Methods (cont'd.)

Java Programming, Sixth Edition


8 Creating Methods (cont'd.)

Java Programming, Sixth Edition


9 Adding Parameters to
 Methods
Arguments
 Data items you use in a call to a method
 Parameters
 When the method receives the data items
 Implementation hiding
 Encapsulation of method details within class
 Calling method needs to understand only interface to called method
 Include within method declaration parentheses
 Parameter type
 Local name for parameter

Java Programming, Sixth Edition


10 Adding Parameters to
Methods (cont'd.)

Java Programming, Sixth Edition


11 Adding Parameters to
Methods (cont'd.)
 Local variable
 Known only within boundaries of method
 Each time method executes
 Variable redeclared
 New memory location large enough to hold type set up
and named

Java Programming, Sixth Edition


12 Creating Methods that
Require Multiple
Parameters
 Method can require more than one parameter
 List arguments within call to method
 Separate with commas
 Call a method
 Arguments sent to method must match parameters
listed in method declaration by:
 Number
 Type

Java Programming, Sixth Edition


13 Creating Methods that
Require Multiple
Parameters (cont'd.)

Java Programming, Sixth Edition


14 Creating Methods that
Require Multiple
Parameters (cont'd.)

Java Programming, Sixth Edition


15 Creating Methods that
Return Values
 return statement
 Causes value to be sent from called method back to
calling method
 Return type can be any type used in Java
 Primitive types
 Class types
 void
 Returns nothing

 Method’s type
 Method’s return type

Java Programming, Sixth Edition


16 Creating Methods that
Return Values (cont'd.)

Java Programming, Sixth Edition


17 Creating Methods that
Return Values (cont’d.)
 Unreachable statements (dead code)
 Logical flow leaves method at return statement
 Can never execute
 Causes compiler error

Java Programming, Sixth Edition


18 Chaining Method Calls

 Any method might call any number of other


methods
 Method acts as a black box
 Do not need to know how it works
 Just call and use result

Java Programming, Sixth Edition


19 Chaining Method Calls
(cont'd.)

Java Programming, Sixth Edition

You might also like