• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how this line works?

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robot is a class with many methods
where goes the input in the coles ()
?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic