File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes method argument - Whole Object or individual attributes?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method argument - Whole Object or individual attributes?? " Watch "method argument - Whole Object or individual attributes?? " New topic
Author

method argument - Whole Object or individual attributes??

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Suppose I have this class:


I have a Utility class with following methods:


My question is, instead of String name/int id/Date dob - should we pass whole User object? I have seen some developers always passing the whole object instead of individual values. In such cases syntax is:




-Varun -
(My Blog) - Online Certifications - Webner Solutions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

I don't see the point in passing an entire User object if all you need is one of its values.

If you get your date of birth from another source, you would have to create a User object with bogus values for the other fields.


With both approaches you can mimic the other approach:

See why I'd prefer just having a Date object? Remember: you can get a Date from a User quite easily, but to get a User from a Date you need to create that new User.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Thanks Rob
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: method argument - Whole Object or individual attributes??