cell - Java pass by value clarified -
this question has answer here:
i have list of cell object represent board game inside board class.
cell boardgame[][] = new cell[8][8]; i needed temporary cell try player move on him , compare other cells, though use java pass-by-value it.
test(board.boardgame); board.printboard(); private static void test(cell[][] boardgame) { cell c = new cell((new soldier(chesspiece.queen, color.white)), 7, 4); boardgame[7][7] = c; } i read post java here, apparently still didn't catch 100%.
i expected see 1 white queen on board, saw two. know if pass reference can change values, though if pass array members won't modified unless execute return.
please me understand subject better. thanks
edit:
i think don't understand when called attributes , doesn't. though different if call "new" or not.
when part of object called attribute right? every object can created part of object. can create new string in dog class , create dog class in animal class , create in class. top class in stack?
for exemple:
public class board { //fake class int num= 0; public void test(int i){ = 1; } } and on class:
public class main { static void outsidetest(board board){ board.num = 1; } public static void main(string[] args) { board board = new board(); system.out.println(board.num); board.test(board.num); system.out.println(board.num); outsidetest(board); system.out.println(board.num); } } now didn't understand why on test() method num didn't change , on outsidetest() num change, num been created in heap because part of board object, need changed on both cases no?
java "pass-by-value".
caveat: passes value stored in memory variable.
for primitive data types, memory allocated in stack space, whereas objects reference memory allocated in stack space object created in heap. similar can stated arrays though not objects in strong sense.
diagrams below make clearer.
your cell object 2d array should seem anobjectarrayvar (not ref in diagram pointing objects should pointing rows , need level of allocation in heap in between ref , objects each row (a set of cells refering objects).
so, when pass boardgame, value stored in stack passed stores reference array of objects (just value stored in anobjectarrayvar). if list of refs stored in location numbered 50 anobjectarrayvar have stored , passes value test method when call it. in such scenario test method wont able goto memory location anobjectarrayvar , change value (to 100) has copy of value change refers to(directly or indirectly) values in ref or next level (and add new objects in case adding new cell queen) or objects pointed them , those changes reflect through out program!
i draw attention fact code
boardgame[7][7] = c; would replace current cell (as soldier in it) create major issues if there soldier in place @ point in game. game state change.
as suggestion (given limited knowledge design) @ least save cell in other value in test method before replacing it.
cell old = boardgame[7][7]; //now operations boardgame[7][7] = old;//just before returning function 

Comments
Post a Comment