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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

Your email address will not be published. Required fields are marked *