this keyword
Composition
References & values
Comparing objects
Enum types
static keyword
public class Car {
private int topSpeed;
Car() {
topSpeed = 100;
}
public setTopSpeed(int topSpeed) {
topSpeed = topSpeed
}
public int getTopSpeed() {
return topSpeed;
}
}Car bmw = new Car();
int speed = bmw.getTopSpeed();What is the value of speed?
Car bmw = new Car();
bmw.setTopSpeed(200);
int speed = bmw.getTopSpeed();100
What is the value of speed?
100
public class Car {
private int topSpeed;
public setTopSpeed(int topSpeed) {
this.topSpeed = topSpeed
}
public int getTopSpeed(int topSpeed) {
return topSpeed;
}
}public class Car {
private int topSpeed;
Car() {
topSpeed = 100;
}
public setTopSpeed(int topSpeed) {
this.topSpeed = topSpeed
}
public int getTopSpeed() {
return topSpeed;
}
}Car bmw = new Car();
int speed = bmw.getTopSpeed();What is the value of speed?
Car bmw = new Car();
bmw.setTopSpeed(200);
int speed = bmw.getTopSpeed();100
What is the value of speed?
200
Until now:
public class Car {
...
private String driver;
...
}What if every driver has not only name but points?
What if every driver has not only name but points?
One way:
public class Car {
...
private String driver;
private int driverPoints;
...
}What if in the future we decide to add age, category, number of games played...?
Separate driver as a new class
public class Driver {
private String name;
private int points;
public Driver(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}Change driver type in class Car
public class Car {
private Driver driver;
public Driver getDriver() {
return driver;
}
public void setDriver(Driver driver) {
this.driver = driver;
}
}Primitives vs References
How Java stores primitives
How Java stores objects
References
NO
Car nissan1 = new Car("Nissan GT-R");
Car nissan2 = new Car("Nissan GT-R");Does nissan1 == nissan2 ?
Car nissan1 = new Car("Nissan GT-R");
Car nissan2 = new Car("Nissan GT-R");Car nissan1 = new Car("Nissan GT-R");
Car nissan2 = new Car("Nissan GT-R");
nissan1 = nissan2;->
Car nissan1 = new Car("Nissan GT-R");
Car nissan2 = new Car("Nissan GT-R");
nissan1 = nissan2;Does nissan1 == nissan2 ?
YES
Examples:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}Usage example:
Day day1 = Day.MONDAY;
Day day2 = Day.TUESDAY;Day day1 = Day.MONDAY;
Day day2 = Day.TUESDAY;
Day day3 = Day.MONDAY;Comparison
Does day1 == day2 ?
NO
Does day1 == day3 ?
YES
switch (day) {
case MONDAY:
...
break;
case FRIDAY:
...
break;
case SATURDAY: case SUNDAY:
...
break;
default:
...
break;
}Using enum for car color in Wacky Racers - green, red, blue, yellow
public enum CarColor {
Yellow, Green, Red, Blue
}public class Car {
...
private CarColor color;
public void setCarColor(CarColor color) {
this.color = color;
}
public CarColor getCarColor() {
return color;
}
...
}Applies to fields and methods
Means the field/method
Is defined for the class declaration
Is not unique for each instance
public class Driver {
...
public static int numOfDrivers = 0;
...
}Driver driver1 = new Driver();
Driver driver2 = new Driver();
driver1.numOfDrivers = 4;What is the value of the field numOfDrivers on driver1?
4
What is the value of the field numOfDrivers on driver2?
4
public class Driver {
...
private static int numOfDrivers = 0;
...
public static int getNumOfDrivers() {
return numOfDrivers;
}
}Call a static method:
...
int driversNum = Driver.getNumOfDrivers();
...What is the value of driversNum?
0
public class Driver {
...
private static int numOfDrivers = 0;
...
public Driver() {
numOfDrivers++;
}
...
public static int getNumOfDrivers() {
return numOfDrivers;
}
}Where to increment numOfDrivers?
In the constructor