This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes when to use property or parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use property or parameter?" Watch "when to use property or parameter?" New topic
Author

when to use property or parameter?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello,

is there a rule (or heuristic) when to set up a property instead of using a parameter in methods signature?

for example:







Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Never heard of such a rule. In the first case, the object "knows" whether it needs to be updated. In the second case, you are telling the object it needs updating willy-nilly. Different designs.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Different designs...yes indeed..and I have the problem to decide which design is better.

What do you think?

In functional manner I would take design nr. 2 ("update" as a parameter) - so I do not need to have a getter/setter for "update" in a object - what do you think?
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
That deppends on who has the knowledge, who says that that object has to be updated? The User or the program?

In the first case the User asks itself 'hey, do I have to update?' wheras in the second the program says 'hey, do I have the conditions?... yes... then you update!'

Both are ok deppends on your application.

Other thing, why do you use Boolean instead of boolean?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
That deppends on who has the knowledge, who says that that object has to be updated? The User or the program?


The User can set the property too, instead of using a parameter..is it good to encapsulate all in properties instead of using obsolete parameters?


I use Boolean because everyman says.."hey use the Wrapper Objects instead of primitive types"..I do not really know, when to prefer the one or the other. However, look at other programming languages such as scala where everything is (or should be) a object. So I use always the wrapper types, in this case Boolean instead of boolean. Is that bad?


Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
nimo frey wrote:
That deppends on who has the knowledge, who says that that object has to be updated? The User or the program?


The User can set the property too, instead of using a parameter..is it good to encapsulate all in properties instead of using obsolete parameters?


What do you mean with obsolete parameters?
I think both are correct, you have to think in the flow of your application and look who has to order the object to be updated.

nimo frey wrote:
I use Boolean because everyman says.."hey use the Wrapper Objects instead of primitive types"..I do not really know, when to prefer the one or the other. However, look at other programming languages such as scala where everything is (or should be) a object. So I use always the wrapper types, in this case Boolean instead of boolean. Is that bad?




I have never heard that about using the Object instead of the primitive, if you look at the Boolean class http://java.sun.com/javase/6/docs/api/java/lang/Boolean.html it's doing references to the primitive type all the time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use property or parameter?