0

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?
}
miXo
  • 193
  • 11
  • 1
    Also read [What is the difference between a variable, object, and reference?](http://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference) – Sotirios Delimanolis Sep 09 '15 at 16:51
  • @SotiriosDelimanolis: I have allready read teh post "Is Java “pass-by-reference” or “pass-by-value”?" It does not answer my question what happens if I save the referenced parameter in an instance variable. – miXo Sep 09 '15 at 17:01
  • The exact same thing that happens when you pass an argument of a reference type to a method. A copy of its value is stored in the corresponding parameter (or variable in your case). – Sotirios Delimanolis Sep 09 '15 at 17:02
  • @SotiriosDelimanolis: Wait... you mean the actual value gets coppied or the value which is actually an address of a variable? – miXo Sep 09 '15 at 17:10
  • Read the link from my first comment. It's all there. – Sotirios Delimanolis Sep 09 '15 at 17:11
  • Objects and Arrays are not _passed_ at all. "Pass by value" and "pass by reference" are all about the relationship between variables and function arguments when a function is called. But in Java, variables and function arguments can not hold objects or arrays. Objects and arrays can only exist on the heap. Java variables can only hold primitive values or references to objects and arrays. When you call a function and pass it an "array", what you are really passing is a reference to the array, and how you are passing that reference is "by value". – Solomon Slow Sep 09 '15 at 18:12

0 Answers0