8.3. The Non Default Constructor

Explicit constructors in a class are called non default constructors.

Explicit default constructor: is an implementation of default constructor.A class can provide explicit default constructor to ensure that any object created will be initialized to the intial default values.

If a class defines any explicit constructors, it can no longer rely on the implicit default constructor to set the state of its objects. If such a class requires a default constructor, its implementation must be provided.

Let’s modify the Light class to have an explicit default constructor.

			package javatutorial;

			/**
			 *  Example class to learn about default constructors, non default constructors 
			 *  and overloaded constructors in Java.
			 *  
			 *  @author f5java.com
			 */
			public class Light {
				
				// Fields:
				int noOfWatts; // wattage
				boolean indicator; // on or off
				String location; // placement
				
				// 1. Explicit default constructor, used to set initial values of fields	
				Light(){
					this.noOfWatts = 100;
					this.indicator = true;
					this.location = "Living Room";
				}

				public static void main(String[] args) {
					// Instantiate and call the method
					
					Light light = new Light(); // call to Explicit default constructor
					light.printLightState();

				}
				
				/**
				 * Prints the state of light object
				 * 
				 */
				public void printLightState(){
					System.out.println("Printing current state of Light");
					System.out.println("No of Watts: " + noOfWatts);
					System.out.println("Is light on: " + indicator);
					System.out.println("Location of light : " + location);
				}

			}
		

Let’s modify the Light class again to have only non default constructor.

			package javatutorial;

			/**
			 *  Example class to learn about default constructors, non default constructors 
			 *  and overloaded constructors in Java.
			 *  
			 *  @author f5java.com
			 */
			public class Light {
				
				// Fields:
				int noOfWatts; // wattage
				boolean indicator; // on or off
				String location; // placement
				
				// 1. Only non default constructor provided, In this case the compiler won't 
				// provide a default constructor. Object instantiation can only be done thru
				// non default constructor
				
				Light(int noOfWatts, boolean indicator, String location){
					this.noOfWatts = noOfWatts;
					this.indicator = indicator;
					this.location = location;
				}

				public static void main(String[] args) {
					// Instantiate and call the method
					
			//		Light light = new Light(); // Invalid, compiler won't allow / compile time error
					Light light = new Light(200,false,"Reading Room"); // Valid, using non default constructor
					light.printLightState();

				}
				
				/**
				 * Prints the state of light object
				 * 
				 */
				public void printLightState(){
					System.out.println("Printing current state of Light");
					System.out.println("No of Watts: " + noOfWatts);
					System.out.println("Is light on: " + indicator);
					System.out.println("Location of light : " + location);
				}

			}