java - How to store a variable so that it can't be modified? -


sorry if format of question wrong, first post.

essentially problem assign object variable, change original object , try , use variable restore object original conditions. however, find, despite never calling method on variable variable has changed same whatever object looks like.

the object try store b , variables seemingly changed both store , original. call methods on original none should change it, recieve information it. call no methods on store.

how, when try perform b = store; ensure b becomes same b passed in parameters?

the code method here:

public int getmove(board b, int playernum) throws quitgameexception{             original = b;     store = b;     int otherplayernum = 0;     try{         out.println("in house 2 on our side " + original.getseeds(2, playernum));     } catch (exception e){         out.println("problem");     }     try{         out.println("in house 2 on our side " + store.getseeds(2, playernum));     } catch (exception e){         out.println("problem");     }       //prints current board user in textual interface     if(playernum == 1){         otherplayernum = 2;     } else {         otherplayernum = 1;     }      out.println("opponent's side: ");     for(int i=6;i>0;i--){         try{             out.print("house " + + " : [" + original.getseeds(i, otherplayernum)+ "] ");         } catch (invalidhouseexception e){             out.print("invalid house");         }     }     out.println();     out.println("opponent's score: " + original.getscore(otherplayernum));      out.println("computer's side: ");     for(int i=1;i<7;i++){         try{             out.print("house " + + " : [" + original.getseeds(i, playernum) + "] ");         } catch (invalidhouseexception e){             out.print("invalid house");         }     }     out.println();     out.println("computer's score: " + original.getscore(playernum));      //each move tried score can received, move produces highest score chosen returned.     system.out.println(b.tostring());     int highestscore = b.getscore(playernum);     int besthouse = 0;     boolean movefound = false;     int move = 1;     int score = 0;     for(int =1; i<7 ;i++){         try{             b.makemove(i,playernum);             score = b.getscore(playernum);         } catch (exception e){             out.println("a problem");             score = 0;         }          if(score>highestscore){             besthouse = i;             highestscore = score;             movefound = true;             move = i;         }         try{             system.out.println("seeds in side " + playernum + " , house " +i+" = "+original.getseeds(i, playernum));         } catch (exception e){             out.println("problem");         }                     b = original;     }     try{         out.println("in house 2 on our side " + original.getseeds(2, playernum));     } catch (exception e){         out.println("problem");     }     try{         out.println("in house 2 on our side " + store.getseeds(2, playernum));     } catch (exception e){         out.println("problem");     }     out.println("all okay? "+b.equals(store));     out.println("all okay? "+original.equals(store));     int side = playernum;     int bestscore = 0;     int seeds = 0;      //if no move has been found increases score first house seed in chosen returned     if(!movefound){         (int =1; i<7 ;i++){             try{                 seeds = original.getseeds(i,playernum);                 system.out.println("seeds = "+ seeds);                 if (seeds>0 && !movefound){                     move = i;                     movefound = true;                 }             } catch (exception e){                 seeds = 0;             }         }     }     return move;  } 

thank in advance. happy provide further details.

variables don't hold objects. hold references (or pointers, if prefer) objects. assigning object variable doesn't create copy of object. make variable point object:

board b = new board(); 

this creates board objects, , make b variable point object:

                b ------> board object  board store = b; 

this assigns same board object variable store. both b , storenow point board object.

                b ------> board object                              ^                 store -------| 

so if naw b.setname("new name"), modify state of board object, , since b , store both reference same board object, calling store.getname() return new name:

                b ------> board object new name                              ^                 store -------| 

if want variable hold original board state, need create copy of board object:

board store = new board(b); 

this constructor should copy board takes argument:

public board(board other) {     this.name = other.getname();     ... } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -