I have done some research and I found out that in Java, primitive data types are passed by value and Object
and Arrays
are kind of passed by refernce.
I am not quite sure if I understand exactly, when the variable is passed as refernce and when is it passed as value.
For example if I pass an object to a click listener It is probably passed by reference, right? But what happens if, I then save the value of that parameter in instance variable inside click listener? Is the data beeing copied?
I have example code below (with some more questions in comments):
//some big class with lots of arrays
public class InfoClass{
private ArrayList<SomeObject> array1; //huge array
private ArrayList<SomeObject> array2; //huge array
//methods...
}
//listview menuitem click listener which is applied to all items
public class ListviewItemClickListener implements View.OnClickListener{
private InfoClass _info; //is this a copy of an object or a reference?
private MyArrayAdapter _adapter;
public ListviewItemClickListener(InfoClass info, MyArrayAdapter adapter){
_info = info;
_adapter = adapter;
}
@Override
public void onClick(View view) {
//do something with info class..
_adapter.notifyDataSetChanged(); //update adapter
}
}
//also:
//what about this, is this inefficient?
private class SomeClass{
public String printValueAndObjectType(Object value, Class type){
return value.toString()+" "+type.getName();
}
//example call: printValueAndObjectType("value", String.class);
//is the String.class beeing copied?
}