2.3. Objects : Class Instantiation, Reference Values, and References

Object: An object is an instance of a class. The object is constructed using the class as a blueprint. An object must be created before it can be used in a program.

Class Instantiation: The process of creating objects from a class is called instantiation. This is done using new keyword with call to a constructor in Java.

Reference Values: A reference value is returned when an object is created. A reference value denotes a particular object.

References: A reference is a variable that can store a reference value. A reference thus provides a handle to an object. In Java, an object can only be manipulated via its reference value

The process of creating objects (instantiation) usually involves the following steps:

  1. Declaration of a variable to store the reference value of an object. This involves declaring a reference variable of the appropriate class to store the
  2. Creating an object using new keyword.

Let's modify the Car and Student classes we created earlier, then we'll instantiate the class, create references and identify the reference values

			package javatutorial;

			/**
			 * Example of a class
			 *
			 */
			public class Car {
				// Fields : value in fields represent the state of the class
				private boolean ignition;
				private String gear;
				private String fuelTankStatus;
				private boolean musicPlayer;
				
				
				// Methods: represent the behavior of the class
				
				/**
				 * Displays car is moving forward message
				 */
				public void moveforward(){
					System.out.println("Moving the car in forward direction");
				}
				
				/**
				 * Displays car is moving backwards message
				 */
				public void moveBackwards(){
					System.out.println("Moving the car in reverse direction");
				}
				
				/**
				 * Displays car is turning right message
				 */
				public void turnRight(){
					System.out.println("Turning car right");
				}
				
				/**
				 * Displays car is turning left message
				 */
				public void turnLeft(){
					System.out.println("Turning car left");
				}

				/**
				 * Displays car status
				 */
				public void displayCarStatus(boolean ignition,String gear,String fuelTankStatus,boolean musicPlayer){
					System.out.println("Ignition: " + ignition);
					System.out.println("Gear: " + gear);
					System.out.println("Fuel Tank: " + fuelTankStatus);
					System.out.println("Is music player ON? " + musicPlayer);
				}
				
				public static void main(String args[]){
					
					// 1. Reference: carObject is the Reference i.e carObject reference can hold a reference to Car object
					Car carObject; 
					
					// 2. Reference value: the value returned by the new Car();
					// 3. Class instantiation : the process of creating objects
					carObject = new Car();
					
					// 4. All the above can be combined as follows		
					Car anotherCar = new Car();
					
					// 5. Once we create the object, we can call its method (covered in coming chapters)
					carObject.displayCarStatus(true, "4","full", true);
					
				}

			}
		

			package javatutorial;

			/**
			 * Example class, showing declaration of fields and methods in a class
			 *
			 */
			public class Student {
				private String name;
				private String course;
				private String grade;
				
				Student (String name, String course, String grade){
					this.name = name;
					this.course = course;
					this.grade = grade;		
				}
				
				/**
				 * Displays student details
				 */
				public void displayStudentDetails(){
					System.out.println("Name: " + name);
					System.out.println("Course: " + course);
					System.out.println("Greade: " + grade);
				}
				
				public static void main(String args[]){
					// 1. Class instantiation i.e. creating an object of the class 
					Student student = new Student("Sam","Java Programming","A");
					
					// 2. Calling method of a class (covered in coming chapters) 
					student.displayStudentDetails();		
				}

			}