Demo - Passing Objects as Arguments to Methods
The Inventory Class
/**
* This class uses three constructors.
*/
public class InventoryItem
{
private String description; // Item description
private int units; // Units on-hand
/**
* No-arg constructor
*/
public InventoryItem()
{
description = "";
units = 0;
}
/**
* The following constructor accepts a String argument that is assigned to the
* description field.
*/
public InventoryItem(String pDescription)
{
description = pDescription;
units = 0;
}
/**
* The following constructor accepts a String argument that is assigned to the
* description field, and an int argument that is assigned to the units field.
*/
public InventoryItem(String pDescription, int pUnits)
{
description = pDescription;
units = pUnits;
}
/**
* SETTERS
*/
public void setDescription(String pDescription)
{
description = pDescription;
}
public void setUnits(int pUnits)
{
units = pUnits;
}
/**
* GETTERS
*/
public String getDescription()
{
return description;
}
public int getUnits()
{
return units;
}
}
The Java class InventoryItem
in this code is a basic class that represents an inventory item. This class has two instance variables: description
which is a string that describes the item, and units
which is an integer representing the quantity of the item on-hand.
The class provides three constructors:
InventoryItem()
: This is a no-argument constructor. It initializesdescription
with an empty string andunits
with 0.InventoryItem(String pDescription)
: This constructor takes a string argumentpDescription
, and assigns it to thedescription
instance variable. It initializesunits
with 0.InventoryItem(String pDescription, int pUnits)
: This constructor takes a stringpDescription
and an integerpUnits
as arguments. It assignspDescription
todescription
andpUnits
tounits
.
In addition, the class has getter and setter methods for its instance variables:
setDescription(String pDescription)
: This method sets the value of thedescription
instance variable topDescription
.setUnits(int pUnits)
: This method sets the value of theunits
instance variable topUnits
.getDescription()
: This method returns the value of thedescription
instance variable.getUnits()
: This method returns the value of theunits
instance variable.
IInventoryItem Class - Passing on an object as an argument
package OverloadConstructors;
/**
* This program passes an object as an argument.
*/
public class InventoryDemoTwo
{
public static void main(String[] args)
{
// Create an InventoryItem object.
InventoryItem item = new InventoryItem("Wrench", 20);
// Pass the object to the DisplayItem method.
System.out.println("The contents of item are:");
displayItem(item);
// Create another InventoryItem object.
InventoryItem itemTwo = new InventoryItem("Wrench", 20);
// Display the object's contents.
System.out.println("The contents of item are:");
displayItem(itemTwo);
// Pass the object to the ChangeItem method.
changeItem(itemTwo);
// Display the object's contents again.
System.out.println("Now the contents of item are:");
displayItem(itemTwo);
} //end of main
/**
* The following method accepts an InventoryItem
* object as an argument and displays its contents.
*/
public static void displayItem(InventoryItem pItem)
{
System.out.println("Description: " + pItem.getDescription());
System.out.println("Units: " + pItem.getUnits());
}
/**
* The following method accepts an InventoryItem
* object as an argument and changes its contents.
*/
public static void changeItem(InventoryItem pItem)
{
pItem.setDescription("Hammer");
pItem.setUnits(5);
}
}
This Java program is demonstrating how objects can be passed to methods as arguments and how a method can change the state of an object passed to it.
main
method first creates anInventoryItem
objectitem
with a description of "Wrench" and 20 units.Then,
item
is passed to thedisplayItem
method, which accepts anInventoryItem
object as an argument. ThedisplayItem
method prints the description and units of theInventoryItem
passed to it.After that, another
InventoryItem
objectitemTwo
is created with the same values asitem
. It prints the description and units ofitemTwo
directly in themain
method.Next,
itemTwo
is passed to thechangeItem
method, which also accepts anInventoryItem
object as an argument. This method modifies theInventoryItem
it receives by setting the description to "Hammer" and the units to 5.Finally, it again prints the description and units of
itemTwo
. However, this time the values are different because they were modified by thechangeItem
method.
This demonstrates that when you pass an object to a method, the method has access to the original object and can modify its state. This is different from passing primitive types to methods, where the method only gets a copy of the value and cannot modify the original.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark