Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

A constructor's return type cannot be void, but..  RSS feed

 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Report post to moderator
I read from Ashraf Fouad Ayoub's notes:
'A constructor does not declare any return type, <B>not even void</B>. A constructor cannot be final, static or abstract'


The above compiles when a constructor cannot have void return type.
So what exactly is happening here?
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Frankie Chee:
So what exactly is happening here?

A constructor cannot have any return type. Not even void.
In this case, the public void Book() in not a constructor. It is treated as a normal method.
To make things clearer, modify the code as below

Now create a instance of Book as

You won't see the output. Now, remove the void keyword and see what happens.
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you Mani, I am clear now.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Report post to moderator
Arg, a duplicate post.
Please continue this in the duplicate thread.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!