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.
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.
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.
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.
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.