0

In the following code, no matter what int value i give, it always outputs: 0 is thinking...

class Robot { int id; Robot(int i) { id = i; Brain b = new Brain(); b.think(); } private class Brain { public void think() { System.out.println(id + " is thinking"); } } } public class Program { public static void main(String[] args) { Robot r = new Robot(5); } }

16th Sep 2016, 9:47 AM
Chahat Gupta
Chahat Gupta - avatar
4 odpowiedzi
+ 1
Why implementing a Brain class? Just put think() as a public method of Robot.
16th Sep 2016, 10:18 AM
Zen
Zen - avatar
0
I just compiled your code and seems to work fine ... i get "5 is thinking"
16th Sep 2016, 9:57 AM
Alasu Paul Sabrin
Alasu Paul Sabrin - avatar
0
jst make public int id; n then try
16th Sep 2016, 10:17 AM
Bhushan Gaikwad
Bhushan Gaikwad - avatar
0
use this.id=i in constructor u will get the desired output reply if ur problem is resolved I will explain how it happened
19th Sep 2016, 12:55 AM
Ankur Sharma
Ankur Sharma - avatar