Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble setting a Boolean parameter

 
Mohd Asim
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excuse the syntax please. missing ";" and others
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

example:

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

enjoy!
 
Mathias Schneider
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


or



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
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

myMethod(myFoo);

you pass a copy of the address.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is strictly pass by value. See JavaRanch Campfire Story: Pass by Value.
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
arulk pillai
Author
Ranch Hand
Posts: 3385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic