LWN.net Logo

Compiled Java

Compiled Java

Posted Nov 5, 2009 20:59 UTC (Thu) by man_ls (subscriber, #15091)
In reply to: Compiled Java by jengelh
Parent article: Welte: Android Mythbusters (Matt Porter)

As another commenter already said, creating an Integer() would not work. The reason you cannot pass a reference to an int so that it is modified by the function is that you are not expected to do that. And the solution is not to create a class with just an integer attribute and pass that -- the real solution is to not pass integers, but objects with behaviors. Integers are best kept as attributes of objects and handled within those objects. If you really have to change just an int it's best to have the method return a new int. Keeps your code simpler and reduces side effects -- and performance is just as good.

This kind of policy may force you to make things a different way, but it is a perfectly valid way -- it's idiomatic Java if you wish. Now, there are some idiotic policies in Java (like getters and setters, what a waste of time) but keeping methods from modifying their (primitive) parameters seems like a good design choice to me.


(Log in to post comments)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds