+ 1

Please explain this code again

why we are using id=i, necessarily and why we are making the b object?

29th Sep 2020, 8:14 AM
Nishtha
Nishtha - avatar
3 Antworten
+ 7
Nishtha Maybe you were referring to this👇 Kindly check the comments of the code below.. hope you'll understand.. https://code.sololearn.com/cna9v7jS5wFd/?ref=app
29th Sep 2020, 8:24 AM
Minho
Minho - avatar
+ 2
Looks like you asked this question while refering to a specific lesson on sololearn java course. This is Q&A discuss section and it is another section of sololearn's app/website and is not linked to any perticular lesson. in order to make this question complete, kindly add all the necessary details (including the code) in the question description.
29th Sep 2020, 8:22 AM
Arsenic
Arsenic - avatar
+ 1
May be your reffering this below link.. But for clarity share link like this here... https://www.sololearn.com/learn/Java/2173/ There id is Robot Class data property,. You are assigning by constructor a value which is passed. If you remove that, Id value remain 0 in the next print statement.. Next Brain class is inner class and if we want invoke it's its function 'think', we know we can call methods by class or an object. Since it is instance method, (not static) we shloud call it by an object so first we have to create an object so that's why b object is created and invoked the method 'think'. Here if you remove id=i (previous) then you get output " 0 is thinking." With id=I, now output is 1 " is thinking.". Hope it helps...
29th Sep 2020, 9:50 AM
Jayakrishna 🇮🇳