Is it possible to do computation before super() in the constructor?

前端 未结 7 689
深忆病人
深忆病人 2020-12-23 11:17

Given that I have a class Base that has a single argument constructor with a TextBox object as it\'s argument. If I have a class Simple of the following form:



        
7条回答
  •  旧巷少年郎
    2020-12-23 11:45

    It is required by the language in order to ensure that the superclass is reliably constructed first. In particular, "If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass."

    In your example, the superclass may rely on the state of t at construction time. You can always ask for a copy later.

    There's an extensive discussion here and here.

提交回复
热议问题