Basic Object Construction and Use
suggest changeObjects come in their own class, so a simple example would be a car (detailed explanations below):
public class Car {
//Variables describing the characteristics of an individual car, varies per object
private int milesPerGallon;
private String name;
private String color;
public int numGallonsInTank;
public Car(){
milesPerGallon = 0;
name = "";
color = "";
numGallonsInTank = 0;
}
//this is where an individual object is created
public Car(int mpg, int gallonsInTank, String carName, String carColor){
milesPerGallon = mpg;
name = carName;
color = carColor;
numGallonsInTank = gallonsInTank;
}
//methods to make the object more usable
//Cars need to drive
public void drive(int distanceInMiles){
//get miles left in car
int miles = numGallonsInTank * milesPerGallon;
//check that car has enough gas to drive distanceInMiles
if (miles >= distanceInMiles){
numGallonsInTank = numGallonsInTank - (distanceInMiles / milesPerGallon);
System.out.println("Drove " + distanceInMiles + " miles!");
} else {
System.out.println("Could not drive!");
}
}
public void paintCar(String newColor){
color = newColor;
}
//set new Miles Per Gallon
public void setMPG(int newMPG){
milesPerGallon = newMPG;
}
//set new number of Gallon In Tank
public void setGallonsInTank(int numGallons){
numGallonsInTank = numGallons;
}
public void nameCar(String newName){
name = newName;
}
//Get the Car color
public String getColor(){
return color;
}
//Get the Car name
public String getName(){
return name;
}
//Get the number of Gallons
public int getGallons(){
return numGallonsInTank;
}
}
Objects are instances of their class. So, the way you would create an object would be by calling the Car class in one of two ways in your main class (main method in Java or onCreate in Android).
Option 1
Car newCar = new Car(30, 10, "Ferrari", "Red");
Option 1 is where you tell the program everything about the car when you create the object. Changing a property later means calling a method such as paintCar. Example:
newCar.paintCar("Blue");
Note: Pass the type the method declares. You may pass a variable to paintCar as long as that variable is a String.
That was an example of changing properties of an object, receiving properties of an object would require using a method from the Car class that has a return value (meaning a method that is not void). Example:
String myCarName = newCar.getName(); //returns string "Ferrari"
Option 1 is the best option when you have all the object’s data at the time of creation.
Option 2
Car newCar = new Car();
Option 2 has the same result but requires more work to finish the object. The no-argument constructor in Car is:
public Car(){
milesPerGallon = 0;
name = "";
color = "";
numGallonsInTank = 0;
}
You do not pass arguments to create it. That is useful when you do not yet have every property. It stores placeholder values so later getters return those placeholders instead of failing. A void method named Car would not be a constructor.
Note: Do not forget that you have to set the parts of the object later that you did not initialize it with. For example,
Car myCar = new Car();
String color = myCar.getColor(); // returns ""
This is a common mistake amongst objects that are not initialized with all their data. Errors were avoided because there is a Constructor that allows an empty Car object to be created with stand-in variables (public Car(){}), but no part of the myCar was actually customized. Correct example of creating Car Object:
Car myCar = new Car();
myCar.nameCar("Ferrari");
myCar.paintCar("Purple");
myCar.setGallonsInTank(10);
myCar.setMPG(30);
// drive divides by milesPerGallon, so set a non-zero MPG before calling it
And, as a reminder, get an object’s properties by calling a method in your main class. Example:
String myCarName = myCar.getName(); //returns string "Ferrari"