How to change the value of a boolean var inside a method? - java

This is maybe so silly.
I have a boolean variable inside the main method. By calling another method of this class or another class I want my boolean value to be modified in the main method. I do this but the change happens only in the called method(locally), not the caller(main). I think this is because of the pass-by-value feature of java.
I even tried Boolean type, but the same problem there!
Actually I'll use this to manage the ordering of concurrent threads. The main processor will check for the boolean value of every thread to see if it is ok to continue and tick the clock. After ticking the clock the main will make the vars false and will wait until the vars are again true. the sub-threads will start their task if the boolean value of them each is false. After the task is done they will make the vars to true so the main processor is able to tick again.
So I want something without a return. I mean as the value is changed inside the method the main could see it.

boolean var = true;
var = methodWhichReturnsTheNewValueOfTheVariable();
and inside the called method:
return newBooleanValue;
or
SomeObjectWithBooleanVariableInside var = new SomeObjectWithBooleanVariableInside(true);
methodWhichModifiesTheObject(var);
and inside the called method:
var.setBooleanValue(newBooleanValue);
A Boolean is such an object: it contains a boolean value. But it's intentionally designed as immutable: its wrapped boolean value can't be changed. So you need to create your own, functional object.

The usual way to do this is the following:
public static void main(String[] args) {
boolean myVar = true;
...
...
myVar = myFunction();
}
public static boolean myFunction() {
// the method should do it's calculation and return the value:
return false;
}

Yes - you cannot modify passed-by-value parameter inside a method in Java (for example in C# you would write method(ref param)).
Why can't you return this value using the method:
public boolean method(params...) {...}
Or you can pass in param the reference to caller:
public void method(params..., CallerClass caller) {
//do something
caller.setValue(Boolean.FALSE);
}
Or you can make this variable accessible in caller and calling method scopes - static variable, etc.

Primitive types are passed by value, so you can't change variables coming as parameter in a method.
This makes also easier to understand how a program works, since this kind of behavior is made more evident in an invocation like this:
boolean prime = false;
prime = isPrime(number);
you can see that found variable is reassigned; while you can assume that number will remain unchanged. This helps in code readability.
There is a dirty trick that sometime can be used. Since arrays are objects, you can use an array to wrap a primitive type:
boolean[] prime = { false };
isPrime(number, result);
public void isPrime(int number, boolean[] result) {
result[0] = true;
}
The object reference is passed by value too, but here we change the content of the array, not the array reference.
This works. But, I don't recommend to design your code like this.
Sometimes, knowing this trick can be useful in unit tests though.

when you think that you changed the value of the primitive boolean it only changed the value in the scope of that method. same with Boolean as it is immutable. changing its value actually assigned a new value to it inside the scope of that method.
you should return the new value from that method and then assign it or you could also use a global boolean that is known to all and to change that one.
(and by the way, if you're dealing with concurrency check out AtomicBoolean)

Related

How do I use return statements from a method as a parameter to another method?

I'm using a class variable, which I assigned a value in a method return. When I'm trying to use the return value as a parameter for another method it gives an error unless I declare the return statements again, which means I'd be creating a new local variable I'm guessing, and not using the class variable. So is it possible to use the return value, instead of ending up declaring a new variable.
I'm just asking whether it's possible. If it is, then there's probably something else wrong with my code.
When you have method returnOne do this:
public int returnOne() {
return 1;
}
And you have another method isOne like this:
public boolean isOne(int number) {
return (number == 1)
}
You can use the result of returnOne in isOne as a parameter like this:
boolean result = isOne(returnOne());
Hope this helps!

JUnit/Mockito - How to capture changes in an argument's field?

I have an ObservableBoolean which is a pretty simple data container wrapping a primitive boolean. I want to use JUnit/Mockito to verify that the primitive boolean's value changes to true followed by false. I am setting up a mock OnPropertyChangedCallback like so:
Observable.OnPropertyChangedCallback loadingCallback = mock(Observable.OnPropertyChangedCallback.class);
viewModel.loading.addOnPropertyChangedCallback(loadingCallback);
I am then running the code under test which does:
loading.set(true);
loading.set(false);
Each of these calls fires the onPropertyChanged method on the loadingCallback, passing the same ObservableBoolean instance as an argument.
I've tried using an ArgumentCaptor and a custom ArgumentMatcher. Both methods successfully show that the onPropertyChange method has been called twice, however, neither of them maintain a history of what happened to the primitive boolean within the ObservableBoolean. Instead, I can only see the most recent value set on it, which is false.
How can I maintain a history of what happened to the primitive boolean within the ObservableBoolean? Again, I simply need to verify that it was set to true followed by false.
You could create a stub which implements Observable.OnPropertyChangedCallback and records the invocations and then provides a public accessor to these recorded invocations. Your test would then use the public accessor to these recorded invocations when asserting.
For example:
OnPropertyChangedCallbackStub loadingCallback = new OnPropertyChangedCallbackStub();
viewModel.loading.addOnPropertyChangedCallback(loadingCallback);
// invoke the code-under-test which, internally, invokes ...
// loading.set(true);
// loading.set(false);
assertTrue(2, loadingCallback.recordedInvocations().size());
assertTrue(loadingCallback.recordedInvocations().contains(true));
assertTrue(loadingCallback.recordedInvocations().contains(false));
I'm posting the implementation of the class #glytching suggested in his answer:
val callback = BooleanObservableCallback()
viewModel.isLoading.addOnPropertyChangedCallback(callback)
viewModel.onLoginButtonClicked()
assertThat(callback.recordedInvocations, hasSize(2))
assertThat(callback.recordedInvocations[0], `is`(true))
assertThat(callback.recordedInvocations[1], `is`(false))
class BooleanObservableCallback : Observable.OnPropertyChangedCallback() {
private val _recordedInvocations = mutableListOf<Boolean>()
val recordedInvocations: List<Boolean>
get() = _recordedInvocations.toList()
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
if (sender is ObservableBoolean) {
_recordedInvocations.add(sender.get())
}
}
}

Passing and setting booleans - Java/Android

I have a class like so:
public class Achievements(){
boolean score100_Earned_Offline;
boolean score1000_Earned_Offline;
final String score100 = "xxxxxxxxxx" //where xxxxxxxxxx would be replaced with the achievement's Google Play Games ID
final String score1000 = "xxxxxxxxxx" //where xxxxxxxxxx would be replaced with the achievement's Google Play Games ID
}
In my Game class, I check the state of the achievements every tick and act on them as necessary like so (assume all methods to be valid and defined - this is cut down to provide the code necessary to the question).......
public class Game(){
public void checkAchievements(Achievements achievements){
if (score>=100){
unlockAchievement(achievements.score100, achievements.score100_Earned_Offline);
}
if (score>1000){
unlockAchievement(achievements.score100, achievements.score1000_Earned_Offline);
}
}
public void unlockAchievement(String achievementToUnlock, boolean thisAchievementOfflineFlag){
//If user is signed in, then we are ready to go, so go ahead and unlock the relevant achievement....
if (checkSignedIn()){
Games.Achievements.unlock(getApiClient(), achievementToUnlock);
//Otherwise, I want to do is set the relevant flag to true so it can be checked when the user does eventually log in
else{
thisAchievementOfflineFlag=true;
}
}
}
Pass by value
In the 'unlockAchievement' method, the boolean 'thisAchievementOfflineFlag' does get set to true if the user is not logged in, however, it doesn't effect the actual boolean that was originally sent into the method (which as you can see is defined in my 'Achievements' class). I'm guessing this is because Java is Pass by Value and is therefore, creating a local copy of the variable which is valid inside the method only. I did try using Boolean too (wrapper class) but got the same results.
Other ways to achieve this?
I've currently got it set up so I can define each achievement as an enum so each one will have it's own copy of the boolean flag. However, I'm aware that it's not recommended to use enums in Android so if there is a better way that I am missing, I would rather avoid them.
Please note that I don't want to use if checks or switch statements as this is taking place in a game-loop.
Any suggestions appreciated
This is all because Java's implementation of Boolean (also, for example String) is immutable for safety reasons. You can see it here: http://www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/
You can solve your problem by introducing an object wrapper for that boolean:
public class BooleanWrapper {
private boolean value;
public void set(boolean value) {
this.value = value;
}
public boolean get() {
return value;
}
}
Now, this object reference will be passed by value but will still point to the same BooleanWrapper object on the heap. You can simply use getters and setters to change the inner boolean value.
Then your code would become:
public void unlockAchievement(String achievementToUnlock, BooleanWrapper thisAchievementOfflineFlag){
if (checkSignedIn()){
Games.Achievements.unlock(getApiClient(), achievementToUnlock);
else {
thisAchievementOfflineFlag.set(true);
}
}
Java is pass-by-value:
When you pass boolean then you for sure passed it by value, while it is a primitive type. When you pass Boolean, you would think it's an object and that you can change it's state, but actually you cannot because Boolean is implemented as an immutable object (as already said). You can confirm this just by reading the code of java.lang.Boolean.
But if you create your own wrapper class, and in a sense, you control whether you implement it in immutable or mutable way. BooleanWrapper I wrote lets you change the state of that object. And when you pass an object such as this one to the method, it's passed by value. That means that another reference is created, but it points to the same object on heap (see image below).
You could use an AtomicBoolean, which will have pass-by-reference semantics.

JMockit : Inspect local variables of a method

Is it possible to inspect local variables of a method in JMockit?
Source
void foo(){
boolean isPresent = false;
/// ... something here sets isPresent
}
Test
can I check the value of isPresent at the end of call of foo() using JMockit?
Rather than trying to do some obscure mocking mechanism. Try refactoring the code to something that you can test:
void foo(){
boolean isPresent = isPresent();
}
boolean isPresent(){
....
}
Also, consider this. If the variable's value never escapes the method and does not cause some other effect (which should be testable), why try to test it? Or why is it even there? Testing that a method scope variable's value is x has no value. Testing that the method resulted in y because the variable was x has value.

Refresh Java argument

I am writing a program for a programming game called robocode. The problem is here:
void wallScan(boolean While){
stop();
getStraight();
turnGunRight(90);
if(startabsolute){
straight=true;
}
while (While){
ahead(10000000);
turnRight(90);
}
resume();
}
You might not understand most of the code as it extends robocode.Robot, but my problem is in the variable While. The loop doesn't end as the method gets the argument once and it is true so the method becomes an eternal loop but is there a way to refresh the method argument as I don't want to make a while loop every time I call this method?
You shouldn't write you parameters in capital letters. So it would be while instead of While. However this isn't allowed because while is a keyword. So first change your argument passed in the method.
Then your problem is, that you call the method with the argument. Since it is a primitive boolean value you pass, the value can't be changed from another method, call, class, etc. during the execution of your wallScan method and therefore the while loop never finishes.
Instead you should for example create a member field in the class containing this method an give it a meaningful way. in the example i just call it whileCondition.
void wallScan(){
stop();
getStraight();
turnGunRight(90);
if(startabsolute){
straight=true;
}
while (whileCondition()){
ahead(10000000);
turnRight(90);
}
resume();
}
public void setWhileCondition(boolean bool) {
whileCondition = bool;
}
public boolean isWhileCondition() {
return whileCondition;
}
So you can set the condition which leads to the termination of the while loop from outside your method.
It seems to me that you don't want a single boolean value - you want something which will return you a boolean every time you ask for one. As a simple example:
public interface ContinueChecker {
boolean shouldContinue();
}
(Horrible names, but hopefully you can come up with something better.) You can then write:
void wallScan(ContinueChecker checker) {
...
while (checker.shouldContinue()) {
...
}
}
An alternative form of this would be a generic interface, such as Provider<T> one from Guice:
public interface Provider<T> {
T get();
}
Your method could take a Provider<Boolean> for the same purpose.
Personally I prefer this approach over that of Sebi - it allows your class to represent the state of the board itself (or whatever) - whether one particular robot should stop doesn't feel like it should be part of the same state. It's effectively local to this method, as far as I can see.

Categories