Tuesday 26 February 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...