Default Constructor

Default Constructor

  • Every class must have constructor either user define or compiler
    define(default constructor.
  • Constructor define by programmer is known user define constructor.
    where as constructor define by compiler is known as default constructor.
Default constructor Example.

class Sample2
 {
   
	int k=12;
	
	double d;
	
        /*	
         this is a default constructor it defines automatically by compiler 
         and it initialize default value value
   
         Sample2()
	  {
		d=0;
	  }
        */
 }
public class DefaultC 
  {
	public static void main(String[] args)
	{
	System.out.println("**************************");	
	
	Sample2  obj1=new Sample2();
	
	System.out.println("k value  "+obj1.k);
	

	System.out.println("d value  "+obj1.d);
	
        System.out.println("above d value initialize by compiler");	
	

	System.out.println("**************************");	
    	
	}
}
output:
**************************
k value 12
d value 0.0
above d value initialize by compiler
**************************

Leave a Reply