-5

Can anyone explain to me why and when you use objects in method parameters. Example public void carMove(Car c) Can you link me to any tutorials or books that explain these types of problems in detail for me thankyou, I am following these tutorials on kilobolt: http://www.kilobolt.com/day-10-inheritance-interface.html

Thank you

user3473184
  • 23
  • 1
  • 6

1 Answers1

1

Well the reason should be comprehended easily. It is just a simple way to pass information inside that method. In this manner, you are creating a method variable, who has a reference to the object being passed as an argument when the method is called. And you can use this information inside the method (i.e: the state of the object, use its method and so on).

Example:

public void carMove(Car c){
   System.out.println(c.toString() + " + c.hashCode());  //this will print some basic information about that object, based on the methods output
   }

Please take a look at this links: Passing Information to a Method | Defining a method

NOTE: It is almost the same as with primitive data types such as int, long and so on. There is just one slight difference ,Java passes arguments by value (in case of primitives, just their values, in case of objects, the value of the reference pointing to that object). For more on this: Pass-by-reference

Community
  • 1
  • 1
Endrik
  • 2,238
  • 3
  • 19
  • 33