Objects and Classes ISYS350 Instructor: Lei Jin
The syntax for declaring instance variables private primitiveType|ClassName variableName;
Examples private private private private
double price; int quantity; String code; Product product;
How to code instance variables • An instance variable may be a primitive data type, an object created from a Java class such as the String class, or an object created from a user-defined class such as the Product class. • To prevent other classes from accessing instance variables, use the private keyword to declare them as private. • An instance variable should be defined outside of any methods.
Where you can declare instance variables public class Product { //common to code instance variables here private String code; private String description; private double price; //the constructors and methods of the class public Product(){} public void setCode(String code){} public String getCode(){ return code; } public void setDescription(String description){} public String getDescription(){ return description; } public void setPrice(double price){} public double getPrice(){ return price; } public String getFormattedPrice() { return formattedPrice; }
}
//also possible to code instance variables here private int test;
Create Constructors • constructors are used to initiate new objects of that class • constructors must have the same name as the class has • Constructors do not have a return type—not even void • You should usually create at least one default constructor for each classes • you can have more than one constructors as long as they have different parameter profiles
The syntax for coding constructors public ClassName([parameterList]) { // the statements of the constructor }
A constructor that assigns default values public Product() { code = ""; description = ""; price = 0.0; }
A custom constructor with three parameters public Product(String code, String description, double price) { this.code = code; this.description = description; this.price = price; }
Another way to code the constructor shown above public Product(String aCode, String aDescription, double aPrice) { code = aCode; description = aDescription; price = aPrice; }
How to refer to instance variables with the this keyword public Product(String code, String description, double price) { this.code = code; this.description = description; this.price = price; }
How to use the this keyword • Since Java implicitly uses the this keyword for instance variables and methods, you don’t need to explicitly code it unless a parameter has the same name as an instance variable.
Accessor and Mutator methods public class Circle { private double radius; … public double getRadius() { return radius; } public void setRadius(double aRadius) { radius = aRadius; } } •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 !
Declaration: the name rect will be used to refer to a Rectangle object
Initiation: Rectangle() is a call to Rectangle’s constructor, which initializes the object.
Rectangle rect = new Rectangle(1,1,4,4) Instantiation: creates the new object (allocates memory space for it.
Access Instance Variables and Invoke Instance Methods • Access private instance variables objectname.getVariableName() circle1.getRadius() myProduct.getPrice()
• Calling instance methods objectName.methodName(argumentList); or objectName.methodName(); myRect1.move(50, 50); rectangle1.findArea();
toString() method • Java library provides a method called toString() that returns the String representation of the object on which it is called. • When you do System.out.println() an object, the toString() method of the object is automatically called. Product product1 = new Product("java", "Murach's Beginning Java 2", 49.5); System.out.println(product1); 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: Product product1 = new Product("java", "Murach's Beginning Java 2", 49.5); System.out.println(product1); Output:
Code: java Description: Murach's Beginning Java 2 Price: 49.5