Do not know how to use no-argument constructor and an argument constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Do not know how to use no-argument constructor and an argument constructor

Hi everyone, I'm working on a project that asked in the code to have one no-argument constructor and one argument constructor, but I don't know how to add it with the code I've already written. Would someone know how to do this? https://code.sololearn.com/cO0cuOnBRqp3/#java

29th Sep 2021, 1:44 AM
David
David - avatar
4 Answers
+ 2
Constructor methods should be written with same name as the containing class and are automatically triggered when we create a new object of that class. In your case an example would be Book(){ System.out.println("new Book object"); } Book(String title){ System.out.println("new book with title"+ title); }
29th Sep 2021, 2:00 AM
_rk
_rk - avatar
+ 2
Just like that: (for book class) public Book(){ } public Book(/* argumnets */){ }
29th Sep 2021, 1:50 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Aleksei Radchenkov thank you for your help. I was able to figure it out
29th Sep 2021, 4:10 AM
David
David - avatar
+ 1
_rk Thank you for answering. I got it!
29th Sep 2021, 4:11 AM
David
David - avatar