• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how this line works?

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robot is a class with many methods
where goes the input in the coles ()
?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword "new" creates a new object. What follows "new" is a constructor call for the object.

The parameters (2, 7, 0, East) are passed to the constructor and used to create a specific Robot instance.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what robot it creates
?

there are methods in the function
i dont know what it does with them
is there any example for it?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is an Object Oriented language. That means you create objects that represent things. In this case, someone, somewhere has written a Robot class. There is probably a Robot.java file you can look at.

Calling the constructor creates an instance of the Robot. It's like telling a construction company to "Use this blueprint to go build a house". The values passed in to the constructor are like passing in the colors you want the bedroom painted.

Back to your example, once that constructor returns, you can now call the methods defined in the Robot class by saying

karek.callMethod1();
karek.callMethod2(;

etc.

You could create many robot instances, and have each do their own thing:


and each robot would do its own thing.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these are the methods of the class( sorry the signatures twisted)
i undrerstand your example in which we get an object from which we could
use its method


like you said

but where did you use the data of


?

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values 2, 7, 0, East in the line:

are the arguments for a constructor of class Robot. We can't tell you what these values mean; you'd have to look at the class Robot to understand that. These values don't have anything to do with the methods in class Robot.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so could you give an exaample to a constructor
where you use the input data
?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors work almost the same as regular methods. You pass parameters to them by putting the parameters between parentheses ( and ).

Here's a very simple example of a constructor that takes two parameters and sets member variables to the values of the parameters.

These are really basic concepts. Have a look at the tutorial - it explains what classes and objects are exactly, what constructors and methods are and how you call them.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at a simple object, like a Boolean. To create one, you call (one of) its constructor, and pass in a value:

Boolean myBool = new Boolean("true") ;

In this case, I used the constructor that takes a String. Nowhere in the class does it actually STORE that string, it simply uses it to figure out how to create itself. That's not to say that such params passed to a constructor are NEVER saved - in fact, Jesper shows an example where they are.

The point is, you don't always know, and often don't need to know. That's kind of the point of OO programming - the details are hidden from people who don't need to know them.

Having said that, I would say that if nobody tells you what the values you are passing in are used for in some broad sense, then there is a documentation issue. They could be the starting position on a grid, whether or not it is holding a beeper, and what direction it is facing, for example. Or they could be something else entirely. We can't know without seeing the code or the specs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic