6.3. Initializing Local Reference Variables

Local Reference Variable Initialization: Local reference variables will not get initialized to default values. You have to explicitly initialize the local variable before usage or else compiler reports an error.

			package javatutorial;

			/**
			 * Example class explaining the local variable initialization of reference
			 */
			public class LocalReferenceVariableInit {
				

				Student student; // Not in any method/constructor/block , hence NOT a local variable
				
				public void printMessage(){
					Car car; // local reference variable : uninitialized
					
					// Lets try to print uninitialized instance and local variables
					System.out.println("student reference is " + student);
			//		System.out.println("car reference is " + car); // Compiler wont allow i.e. compile time error
				}

				public static void main(String[] args) {
					// local reference variable: uninitialized
					Employee employee;
					
					// Instantiate and access variable on LocalReferenceVariableInit class		
					LocalReferenceVariableInit localRef = new LocalReferenceVariableInit();
					
					System.out.println("student reference is " + localRef.student);
			//		System.out.println("car reference is " + car); // Compiler wont allow i.e. compile time error

				}

			}