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

前端 未结 7 2500
清酒与你
清酒与你 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:36

    It does. Although the question is based more on usage.

    public class Main {
        String x;
    
        Main() {
            x = "Init";
        }
    
        @Override
        public String toString() {
            return x;
    
        }
    
        public static void main(String[] args) {
            System.out.println(new Main());
        }
    
    }
    

    Ouput:

    Init
    

提交回复
热议问题