Tuesday, February 26, 2019

android - Passing object to method in java appears to be by reference (and Java is by val)

I thought when you passed objects to methods in Java, they were supposed to be by value.



Here is my code:



public class MyClass{
int mRows;
int mCols;

Tile mTiles[][]; //Custom class

//Constructor
public MyClass(Tile[][] tiles, int rows, int cols) {
mRows = rows;
mCols = cols;

mTiles = new Tile[mRows][mCols];
for (int i=0; i < mRows; i++) {
for (int j=0; j < mCols; j++) {

mTiles[i][j] = new Tile();
mTiles[i][j] = tiles[i][j];
}
}
}


At this point, any changes to the mTiles object are reflected back to the tiles object. Any ideas how to fix this?



Thanks all.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...