aspose file tools*
The moose likes Beginning Java and the fly likes how this line works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how this line works?" Watch "how this line works?" New topic
Author

how this line works?

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191

Robot is a class with many methods
where goes the input in the coles ()
?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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

Joined: Aug 16, 2005
Posts: 14350
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok so could you give an exaample to a constructor
where you use the input data
?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how this line works?