File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is assigning "size" that way considered WRONG?

 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is assigning "size" that way considered WRONG?


 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not "considered" wrong, it is incorrect. In the body of the setSize method, "size" refers to the parameter, not to the instance variable. So what that assignment does is assign the parameter to itself - in other words, it does nothing. Especially the value of the instance variable of the same name is not changed.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it won't do what you intend it to: you're assigning a variable to itself, not to the class member.
 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing your setSize() method to this:



Using the 'this' keyword tells the compiler you want to set the instance variable to the value of the parameter.

John.
 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic