This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes trouble setting a Boolean parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble setting a Boolean parameter" Watch "trouble setting a Boolean parameter" New topic

trouble setting a Boolean parameter

Mohd Asim

Joined: Jul 04, 2007
Posts: 24
Hi I am trying to set the value of a Boolean parameter within a method that returns a String as follows:

when i try to obtain the value of this Boolean object using:

I do not get the changed value i.e.

returns false.

what is wrong?

[edit]Disable smilies. CR[/edit]
[ September 04, 2008: Message edited by: Campbell Ritchie ]
Mohd Asim

Joined: Jul 04, 2007
Posts: 24
excuse the syntax please. missing ";" and others
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Java parameters are passed by value -- i.e., the variable inside the method is a copy of the variable outside the method. Assigning a new value to a variable changes the value inside the method only -- the original variable outside the method is unchanged.

Note that if a parameter is a reference to an object, then both the original variable and the copy will point to the same object. Assigning to the copy in the method will make that copy point to a new object, but again, won't affect the original.

[Jess in Action][AskingGoodQuestions]
T.A. Nguyen
Ranch Hand

Joined: Sep 02, 2008
Posts: 36

And if MUTABLE object passed into the method, the code inside the method can use one of the setXXX() method to alter the value of it member(s). But in the case for all primitive data type class are IMMUTABLE and hence can not change it member(s). So, if you must, wrap it around another object.


then you can use the .setValue() method in the MyBoolean to modify your b object.


T.A. Nguyen
Mathias Schneider

Joined: Oct 29, 2008
Posts: 5
Java parameters are passed by value

Thats wrong!!

In Java all parameters are passed by reference, even Strings and booleans!

But if you assign a primitieve type within a method to some value like


then the reference to the object b is changed to a new object. And then you have to know:

Changing references of an object within an method will be lost as soon as the method has finished. This is so because every method just builds an temporary stack for its calculation during the interpretation of the .class file.

There is only one way to maintain changes of refenrences wihtin a method:
you have to return the new reference and catch it to your old reference:

of yourse you can wrap it into an object and change its members with settsers but thats not changing the reference of the object but changing its members.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

Originally posted by Mathias Schneider:
Thats wrong!!

In Java all parameters are passed by reference, even Strings and booleans!

Ummm... nope. EVERYTHING IN JAVA IS PASS BY VALUE. There are no exceptions. When you "pass an object", what you are really doing is passing the value of the reference to the object.

You do not have direct access to object in java. when you say

Foo myFoo = new Foo();

myFoo is a reference to the object, not the object itself. Basically, it's the address of the object. So when you pass it in to a method:


you pass a copy of the address.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber

Joined: Aug 31, 2004
Posts: 11343

Java is strictly pass by value. See JavaRanch Campfire Story: Pass by Value.

"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
Henry Wong

Joined: Sep 28, 2004
Posts: 20538

Also, keep in mind that Java is "pass by value", as defined by Java. This definition of "pass by value" is different from the "pass by value" definition in C/C++. A more desciptive definition is... Java is "pass by value of the object reference". A copy of the reference is passed.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3371
Other languages use pass-by-reference or pass-by-pointer. But in Java no matter what type of argument you pass the corresponding parameter (primitive variable or object reference) will get a copy of that data, which is exactly how pass-by-value (i.e. copy-by-value) works.

In Java, if a calling method passes a reference of an object as an argument to the called method then the passed-in reference gets copied first and then passed to the called method. Both the original reference that was passed-in and the copied reference will be pointing to the same object. So no matter which reference you use, you will be always modifying the same original object, which is how the pass-by-reference works as well.

[] | [Java job hunting know how] | [Java resumes]
I agree. Here's the link:
subject: trouble setting a Boolean parameter
It's not a secret anymore!