• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning reference types, whilst the method has reference types as parameters.  RSS feed

 
Darren Estcourt
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading Oracle's javadoc, and I'm puzzled about a brief section which says that reference types can be returned from a method, as well as primitive types, and the parameters can also be object references of reference types.

I have worked out how to return an object reference, using parameters for primitive types, but I don't understand and nor can I find, an example and/or detailed explanation on how (or why) you would return an object reference, based on the arguments sent from the caller.

This is the section of the javadoc I am referring to :

----------------------------------------------------------------------------------------------------------------------------------------------------------

A method can also return a reference type. For example, in a program to manipulate Bicycle objects, we might have a method like this:


---------------------------------------------------------------------------------------------------------------------------------------------------------

Please can someone provide a link with a more detailed explanation that the javadoc, or simply explain how object references for reference types (e.g Bicycle myBike) can be used, and how
you can manipulate their values?

Still a newb, would appreciate help/advise. Thanks!
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darren Estcourt wrote:... parameters can also be object references of reference types.

This confuses me. Your parameter "Bicycle myBike" is an object reference. Not sure what references of reference is unless they're talking about a collection, e.g. List.
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darren Estcourt wrote:
I have worked out how to return an object reference, using parameters for primitive types, but I don't understand and nor can I find, an example and/or detailed explanation on how (or why) you would return an object reference, based on the arguments sent from the caller.


Look at the collections framework, specifically the Map interface (and associated classes).

The  V get(Object key) pretty much covers what you are askig about.
The method takes an object reference, which represents the key to the Map.  The code in the concrete class will then go off and, using that key, will find the associated value and return it.
That value will also be an object reference.
 
Darren Estcourt
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting, this is exactly the sort of thing I was looking for. It gives me a clear example of how and why this is worth knowing/doing. Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!