05 PassingObjects
05 PassingObjects
Passing Arguments to
Methods
Method
• A method is a collection of statements that are
grouped together to perform an operation.
• A method is always defined inside a class.
• Method signature or method header is the
combination of the method name and the
parameter list.
method header {
method body
}
2
Parts of a Method Header
Method Return Method Parentheses
Modifiers Type Name
return empnum;
}
The above method is an instance method.
The type of result returned is returnType where int means the method
returns an integer result.
3
Parts of a Method Header
Method Return Method Parentheses
Modifiers Type Name
empnum = num;
}
The above method is an instance method.
The type of result returned is returnType where void means the
method does not return a result.
4
return Statement
7
Calling an instance method
A method executes when it is called.
The main method is automatically called when a
program starts, but other methods are executed
by method call statements
A method with a non-void return value type can
also be invoked as a statement.
An invocation of an instance method that returns
an integer value may use an assignment
statement or output statement:
int vnum;
vnum = obj1.getEmpNum();
or
System.out.println ("employee num:" +
8
obj1.getEmpNum());
Calling an instance method
An invocation of a void instance method is
simply a statement:
objectName.methodName();
Example:
obj1.setEmpNum(123);
9
Passing Parameters to Constructors
Any expression that has an appropriate
data type can serve as a parameter:
public static void main (String[] args) {
Employee emp1 = new Employee();
Employee emp2 = new Employee(123); // argument
Employee emp3= new Employee(456, "Ahmad"); // argument
}
public class Employee {
public Employee () { }
public Employee (int num) { // parameters
empnum = num ;}
public Employee (int num, String name) {
empnum = num ;
empname = name;
} 10
Passing Arguments to a Method
Values that are sent into a method are called
arguments.
System.out.print("Input the employee number:");
number = Integer.parseInt(str);
obj1.setEmpNum(number);
The data type of an argument in a method call
must correspond to the variable declaration in the
parentheses of the method declaration. The
parameter is the variable that holds the value
being passed into a method.
5-11
Arguments are Passed by Value
In Java, all arguments of the primitive data
types are passed by value, which means that
only a copy of an argument’s value is passed
into a parameter variable.
Any change to a parameter variable inside a
method, has no affect to the original argument.
5-12
Passing Parameters : primitive type
length: 12.0
width: 5.0
box1.equals(box2);
Address
18
Returning Objects From Methods
import java.util.*;
public Rectangle inputRectangle() {
public class PassObject3 { Scanner inp = new Scanner (System.in);
double newWidth, newLength;
public static void main(String[ ] args) {
Rectangle box3 = new System.out.print ("Enter width : ");
Rectangle().inputRectangle(); newWidth = inp.nextDouble();
System.out.println (“The area: “ + System.out.print ("Enter length : ");
box3.calcArea()); newLength = inp.nextDouble();
19
Returning Objects From Methods
Methods can return references to objects.
Just as with passing arguments, a copy of the
object is not returned, only its address.
Method return type:
20
The FoodItem class
The class diagram that represents a food item at a market
FoodItem
- description: String
- size: double
- price: double
+ FoodItem()
+ setDesc()
+ setSize()
+ setPrice()
+ getDesc()
+ getSize()
+ getPrice()
+ calcUnitPrice()
+ toString()
21
Methods for Class FoodItem
For most classes, we need to provide methods that perform
tasks similar to the ones below.
a method to create a new FoodItem object with a specified
description, size, and price (constructor)
methods to change the description, size, or price of an existing
FoodItem object (mutators)
methods that return the description, size, or price of an existing
FoodItem object (accessors)
a method that returns the object state as a string (a
toString() method).
22
Method Headers for
Class FoodItem
Method Description
public Constructor - creates a new
FoodItem(String, object whose three data fields
double, double) have the values specified by its
three arguments.
Mutator - sets the value of data
public void
field description to the value
setDesc(String)
indicated by its argument.
Returns no value.
public void Mutator - sets the value of data
setSize(double) field size to the value indicated
by its argument. Returns no
value.
23
Method Headers for
Class FoodItem, cont’d.
Method Description
public void Mutator - sets the value of data
setPrice(double) field price to the value
indicated by its argument.
Returns no value.
public String Accessor - returns a reference
getDesc() to the string referenced by data
field description.
24
Method Headers for
Class FoodItem, cont’d.
Method Description
public double Accessor - returns the value
getPrice() of data field price.
25
Class FoodItem
/*
* FoodItem.java
* Represents a food item at a market
*/
public class FoodItem {
// data fields
private String description;
private double size;
private double price;
26
Class FoodItem, cont’d.
// methods
// postcondition: creates a new object with data
// field values as specified by the arguments
public FoodItem(String desc, double aSize,
double aPrice) {
description = desc;
size = aSize;
price = aPrice;
}
29
Testing the class FoodItem
UML class diagram
client TestFoodItem
(application) class
main()
association
relationship
worker FoodItem
class description: String
size: double
price: double
FoodItem()
setDesc()
setSize()
setPrice()
getDesc()
getSize()
getPrice()
calcUnitPrice()
toString()
32