2

When I use the following code in Java 8 within NetBeans it doesn't seem to work well. For example, in one method I create the following:

boolean isGuessCorrect = true;

It is then passed to another method where it can be used. However, if I then have some conditional statement that changes the value it doesn't work. NetBeans actually tells me that it can't be changed. Instead I am forced to use something like this:

int isGuessCorrect = 1;

When I use that approach, treating 1 as true and 0 as false, I have no problems changing the values of the variables and passing them around as needed.

The problem is that, for readability, I would prefer using true/false rather than using digits to store the true/false conditions. Is there something special about them that keeps it from working the way I want?

EDIT: This is a very simple text based game that uses logical control of methods and passing variables to handle events in the game.

public static void main(String[] args)
{
        //Sets the game status and difficulty.
        //Using as a separate method so I don't have these variables overwritten on subsequent runs.
        int newGame = 1;
        int difficulty = 6;
        game_menu1(newGame, difficulty);        
}

The above is what I currently have to use, however, what I initially attempted was:

        boolean newGame = true;

The idea is that, depending on if the game is new or not, different things would occur.

In a later part of the program:

        public static void game_menu2(int newGame, int difficulty, int digit1, int digit2, int choice, int counter)
{
    newGame = 0;

The above is what I am currently using. I change the 1 to 0 to indicate that the game is no longer new. However, if I used

        public static void game_menu2(boolean newGame, int difficulty, int digit1, int digit2, int choice, int counter)
{
    newGame = false;

It doesn't work. I'm sorry, but I would have to change a great deal more since I reference this in several places before I can get the exact error message.

EDIT 2: hmm. Even though I had the problem a few hours ago and last night, after changing the code to use boolean variables where needed I am not seeing the same error code. It's likely that the problem I had was something unrelated, a bug that just looked like it was an issue with boolean itself. It's also possible that I had a final operator in place previously where I am not currently using it. Even though I am very familiar with other languages, I am still learning Java in a classroom setting.

I would accept as a correct answer anyone that can show me the situations where this type of problem would normally be expected to occur.

Elliander
  • 503
  • 1
  • 9
  • 19
  • No, you should be able to reassign a non final boolean variable. Can you show what you tried? – folkol Jul 10 '15 at 18:26
  • Could you please post the complete code? I'm not sure if I got what you wanna mean... – cesarse Jul 10 '15 at 18:27
  • Is the method parameter using the `final` modifier? – mkobit Jul 10 '15 at 18:27
  • We need to see more code. I'm not sure where you've declared either of those variables, and your situation would make more sense if that were clear to us. – Makoto Jul 10 '15 at 18:36
  • So you are saying that you want to send a `boolean`, a local var in one method to another method where it is mutated. You could try wrapping the `boolean` in an Object and sending it. – Abhishek Vasisht Jul 10 '15 at 18:40
  • Sure, but I'll have to redo a bit of code to get back to that error I was one. One sec and I'll edit. – Elliander Jul 10 '15 at 18:47
  • @Makoto What do you mean "I'm not sure where you've declared either of those variables" - As I stated in the question, I declared them in one method, passed to another method, and tried to change it in that other method. – Elliander Jul 10 '15 at 18:48
  • ...and that's why I want to see the actual code. You passed them *both* to another method? That doesn't make sense; Java would complain about a redeclaration of `isGuessCorrect`. If you don't mind, would you please include the code? You'll get more help (and we'll be able to see better context) if you do. – Makoto Jul 10 '15 at 18:53
  • Thanks for posting some code, but I need *more* context. What part doesn't work? Is it a compilation error? Is it a runtime error? Including the full body of the method would be ideal. – Makoto Jul 10 '15 at 19:01

2 Answers2

1

In Java, arguments are passed by reference - or, to be more exact, object references are passed by value.

An exception is made for primitives like int, double, and... bool, which are passed by value.

Thus,

public static void game_menu2(boolean newGame, int difficulty, int digit1, int digit2, int choice, int counter)
{
    newGame = false;

operates on a local copy of newGame which dies with the function.

Any change made to newGame (or difficult, digit1... for that matter) only applies to the local copy and is not visible outside your method.

The Boolean class, as any other class, can be passed by reference; unfortunately Boolean is immutable, so you can't do much with it.

I'd consider wrapping your bool inside a (mutable) class

class BooleanWrapper {
    public bool value
}

and passing that around instead.

Note, however, that I can think of few situations in which passing a bool reference is a good idea, and your is probably not one of them: if I understand your use case correctly, I would just update your flag by hand.

Community
  • 1
  • 1
Tobia Tesan
  • 1,938
  • 17
  • 29
0

Because Java passes arguments by reference, changing the value of a parameter does not change the variable which was used to press a value into that variable. You have at least two alternatives.

  1. Create a class member to hold the value.

  2. Return the new value and do an assignment in the caller method.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268