how to explain return statement within constructor?

后端 未结 6 1555
北海茫月
北海茫月 2020-12-08 23:05

as far as i know , the constructor return nothing , not even void ,

and also

return ;

inside any method means to return void .

6条回答
  •  萌比男神i
    2020-12-08 23:45

    Methods declared with void return type as well as constructors just return nothing. This is why you can omit return statement in them at all. The reason why void return type is not specified for constructors is to distinguish constructor from method with the same name:

    public class A
    {
        public A () // This is constructor
        {
        }
    
        public void A () // This is method
        {
        }
    }
    

提交回复
热议问题