Default constructor does not initialize the instance members of the class?

前端 未结 7 2470
清酒与你
清酒与你 2021-02-13 03:05

I encountered a question that asks \"Which of the following are true about the \"default\" constructor?\"

and an option \"It initializes the instance members of the cla

7条回答
  •  天涯浪人
    2021-02-13 03:28

    Default constructor provides the default values to the object (s) and is normally created by the compiler when no constructor is explicitly defined. e.g.

    class DefaultTest{  
    int id;  
    String name;  
    
    void display(){System.out.println(id+" "+name);}  
    
    public static void main(String args[]){  
    DefaultTest s1=new DefaultTest();  
    DefaultTest s2=new DefaultTest();  
    s1.display();  
    s2.display();  
    }  
    }  
    

    NB: There being no constructor defined the compiler will generate a default constructor that will assign 0 null values to the two objects.

提交回复
热议问题