File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes passing boolean by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing boolean by reference" Watch "passing boolean by reference" New topic

passing boolean by reference

karl holmgren

Joined: Aug 16, 2004
Posts: 4
I seem to have some problems passing Boolean object by reference. Variable
successful does not become false in Main() even though I alter it to that in retrieveNext.

Boolean successful = new Boolean(true);

while(successful.booleanValue() && counter<15){

public double retrieveNext(Boolean successful){
return (Double)(channels[currCh]).get(currSampl);
return -1;

[ October 11, 2005: Message edited by: karl holmgren ]
Julien Grenier
Ranch Hand

Joined: Sep 01, 2005
Posts: 41

Boolean object are immutable so you can't change the value of it.
so since you are returning -1 you could consider check if the returned value is -1 and remove the boolean logic.
marc weber

Joined: Aug 31, 2004
Posts: 11343

As Julien pointed out, primitive wrappers (like Boolean) are immutable. So when you say successful = false, you're actually reassigning the reference to a new Boolean instance (using Java 1.5's autoboxing).

But in addition to this, you need to recognize that method arguments are copies -- local to the scope of the method. So within your retrieveNext method, reassigning the variable "successful" only changes the object that the local variable is pointing to. However, the "successful" variable outside of the method will continue to point to the original object.

(To verify this, experiment using combinations of "successful" and "this.successful" within the method.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Java only has "pass by value", not "pass by reference." However, there are other techniquest to achieve the same effect. The most obvious is to return a boolean. In this situation, it doesn't seem to make sense because retrieveNext() already returns a double. So what does the boolean value represent? It looks like it indicates whether the method succeeds based on some criterion. In such as situation, you might consider throwing an exception. This is particularly ideal if "failure" indicates that the class has entered an invalid state that should not occur. This usually means that something is wrong in your code that is causing somethign to happen that should never happen in the finished product. I think exception handling is an ideal way to deal with "success" and "failure" in such a situation. Another suggestion is to have a member field that indicates success or not. However, this is only helpful to other methods in the same class. Otherwise you need to add a getter method. I think an exception would be a much cleaner way to deal with this than a member variable.

I hope this help. If anything is unclear, please let me know so I can answer any questions you may have.


Java API Documentation
The Java Tutorial
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

try this

Boolean [] successful = new Boolean [] {new Boolean(true)};

while(successful[0].booleanValue() && counter < 15){

public double retrieveNext(Boolean [] successful){
if(channels[currCh].size() > currSampl+1){
return (Double)(channels[currCh]).get(currSampl);
return -1;

Now you are still passing by value since that is all java knows. But this solves your problem by using an array. A wrapper would work just the same.

Hope you spend time now to comprehend completely that Java does not pass by reference. Java does not even assign by reference. there is no 'by reference' in java anywhere.
I agree. Here's the link:
subject: passing boolean by reference
jQuery in Action, 3rd edition