5.2. Reference Variables

Reference variable: A variable that store reference values of objects are called reference variables. Reference variable is used to manipulate the object denoted by the reference value. A reference variable has:

Before using the reference variable, it has to be initialized. Let's use the Car, Student, Employee and PaintFactory Classes we created earlier for declaring and initializing reference variable

Declaration + Initialization: Often in Java programs, declaration of the variable is combined with as follows

			package javatutorial;

			/**
			 * Example class to identify various pieces in variable declaration in reference types
			 * A variable has:
			 *     name – name of the variable.
			 *     reference type – type of the value it holds.
			 *     
			 * @author f5java.com
			 */
			public class ReferenceDeclaration {

				public static void main(String args[]) {
					
					// 1. PaintFactory is type, i.e. variable pf can hold a reference to an object 
					// of type PaintFactory
					
					// 2. pf is the name of the reference variable
					PaintFactory pf = new PaintFactory();
					
					// 3. reference type is Car, reference variable name is myVehicle
					Car myVehicle = new Car(); 
					
					// 4. reference type is Student, reference variable name is myStudent
					Student myStudent = new Student("Sharon", "Java tutorial - reference variable declaration", "A"); 
					
					// 5. reference type is Employee, reference variable name is myEmployee
					Employee myEmployee = new Employee();
					
					// Just for the fun of it, let's call methods on all the objects we created
					System.out.println("Printing Paint Factory details");
					pf.displayCustomerOrder();
					
					System.out.println("Printing Vehicle details");
					myVehicle.moveforward();
					myVehicle.turnRight();
					
					System.out.println("Printing Student details");
					myStudent.displayStudentDetails();
					
					System.out.println("Printing Employee details");
					myEmployee.displayEmployeeDetails();		

				}

			}