Basic Object Construction and Use


Objects 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 " + numGallonsInTank + " 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 String 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 essentially tell the program everything about the Car upon creation of the object. Changing any property of the car would require calling one of the methods such as the repaintCar method. Example:

newCar.repaintCar("Blue");

Note : Make sure you pass the correct data type to the method. In the example above, you may also pass a variable to the repaintCar method as long as the data type is correct.

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 gets the same effect but required more work to create an object correctly. I want to recall this Constructor in the Car class:

public void Car(){
	milesPerGallon = 0;
	name = "";
	color = "";
	numGallonsInTank = 0;
}

Notice that you do not have to actually pass any parameters into the object to create it. This is very useful for when you do not have all the aspects of the object but you need to use the parts that you do have. This sets generic data into each of the instance variables of the object so that, if you call for a piece of data that does not exist, no errors are thrown.

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 = Car.getColor(); //returns empty string

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);

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"

Simplest Possible Class

class TrivialClass {}

A class consists at a minimum of the class keyword, a name, and a body, which might be empty.

You instantiate a class with the new operator.

TrivialClass tc = new TrivialClass();

Local Inner Class

A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

public class localInner1{ 
	private int data=30;//instance variable
 
	void display(){ 
		class Local{ 
			void msg(){System.out.println(data);} 
		} 
		Local l=new Local(); 
		l.msg(); 
	}
	public static void main(String args[]){ 
		localInner1 obj=new localInner1(); 
		obj.display(); 
	}
}

Basic Programs