+ 7
Please Someone should explain this code line by line I don't understand thanks
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(1); } }
6 Respuestas
+ 4
in the Robot class ,an instance of Brain is created first same thing with an instance of Robot in the main method of class Program. how is this possible
+ 3
Definition of classes are just blueprint to later build instance object. As explained by @Enzo, the entry point of your code (where the execution start) is the main() function of the class Program (by convention: you can only have one class with a main() method, and the execution not necessarly start at first line of the code)... So, the Robot class is first called only when you create an instance of it, and so the Robot() method (same name than its class is also convention as entry point of the code class, called 'constructor' as it's the code wich initialize and return the instance), and only at this time will create an instance of its private class Brain, self contained in the Robot instance object expected in the main function...
+ 3
@Sean Douglas wrote: << Generally speaking code is run from top to bottom >>
That was quite a right logic in imperative programming only, but less in functional programming, and more less in event-driven or object oriented programming ^^
On the other hand, it's almost logic to put main() near bottom, after all stuff declaration ;P (in Javascript this is even mandatory to not use variables/functions before their declaration (except for function definition without explicit assignement to a variable name) else you will get an error...
+ 1
This is why I'm not a fan of sololearn constantly putting the Main method at the bottom of its code examples.
Generally speaking code is run from top to bottom, but there are exceptions and Main is one of them. No matter where you put Main (as long as it's a valid position of course) it will be executed first.
So, your code first creates an instance of the Robot class (passing it an ID of 1) which in turn creates an instance of the brain class which outputs some text.
0
There's a lot to explain line for line especially as I'm not sure how much you know but I guess I'll try and you can ask questions after(I also don't know java so there's some guess work)
So the program starts through the program main function at the bottom and in that function it creates an object from the Robot class(created at the top). This new object has a parameter of 1 which is entered into the constructor which is a function with the same name as a class that is ran when you create an object. The constructor for robot takes the integer variable called i (Robot(int i) {) and sets the objects 'id' variable to equal the integer(1 in this case) then in the Robot constructor it creates a object from the Brain class and runs the function called think(from the brain object) which just outputs a message which prints the id of Robot(1) and a string('is thinking')
sry for being bad at explaining stuff :/
0
Im not sure what you mean, the main method is the entry point for the program so it starts there. the brain class is nested inside the robot class so it creates the instance from that.