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); } }
4 Respuestas
+ 1
Why implementing a Brain class? Just put think() as a public method of Robot.
0
I just compiled your code and seems to work fine ...
i get "5 is thinking"
0
jst make public int id; n then try
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