Objects and Classes: ISYS350 Instructor: Lei Jin
Objects and Classes: ISYS350 Instructor: Lei Jin
ISYS350
Instructor: Lei Jin
The syntax for declaring instance variables
private primitiveType|ClassName variableName;
Examples
private double price;
private int quantity;
private String code;
private Product product;
•For each private attributes you created, you should also create one get method
and one set method for that attributes
•The get method is also called accessor method, it should always return the value
of the attributes
•The set method is also called mutator method, its return data type should always
be void
Create Objects from Classes
Java treats a class the same as a data type !
Output: productapp.Product@130c19b
toString() method
• To print out the actual content of product1, you need to override the toString()
method.
public String toString()
{
String report;
report = "Code: " + code + "\n" + "Description: " + description
+ "\n“ + "Price: " + price + "\n";
return report;
}
• Now see what happens when you print out product1:
Output:
Code: java
Description: Murach's Beginning Java 2
Price: 49.5