Question

As far as I know and researched, arrays in Java are not objects but they're reference-types. My doubt is, when I want to return an array should I return a deep copy (like you would do with an object) with a clone() method for example, or can I return the variable countaining the array like it was a simple-type variable (ie. int or double) with a get method? For clarification porpuses, I will insert an example code to expose this situation:

public class List
{
    // Instance Variables ----------
    private int[] list1;
    private int[] list2;

   // Constructors ----------
   public List()
   {
     list1 = new int[0]; list2 = new int[0];
   }
   public List(List x)
   {
       list1 = x.getList1();
       list2 = x.getList2();

    }

    // Get methods
    public int[] getList1()
    {
        return list1;
    }

    public int[] getList2()
    {
        return list2;
    }

    // Set methods
    public void setList1(int size)
    {
        list1 = new int[size];
    }

     public void setList2(int size)
    {
        list2 = new int[size];
    }
      // Compare reference between an array and the instance variables
      public boolean equals (int[] x)
    {
        if ( x == list1 || x == list2)
        return true;
        else
        return false;
    }
}

And now I have a TestClass the uses class List like this:

List listx = new List();
int[] listy = listx.getList2();
boolean test = listx.equals(listy);
System.out.printf("Result: " + test );

With this said, when I use the method equals to see if the two arrays share the same reference or adress, I get always the result true!! Am I breaking OOP basic principals with this? Will I loose control because listy is pointing to listx instance variable? Well, I'm really confused with this and I don't know if this is right(being array a non-instantiable class) or If I should send some kind of deepcopy insted of shallow using a Clone method in other to ensure that all basic OOP principals are fulfilled, and with this principals I mean that the class method should be acessed only by the API and that the internal state(instance variables) can only be acessed by the class itself.

Was it helpful?

Solution

You are not breaking OOP principals. However, you are breaking principals of functional programming. Functional programming views leaking of access as losing of control.

Whether or not you want to practice functional programming is up to you, Java doesn't take a stance in that matter.

You may want to consider if it's important not to leak access for this particular class. If you find it important not to leak access then make this class immutable.

You can also guard the instance variables. In this scenario any possible changes to the variables must be handled by the instance class. However, the instance could be modified from separate contexts and result in loss of control. For this reason functional programming only allows immutable classes.

OTHER TIPS

If you want the invoker of the method to be able to modify the original array, you don't need to do a copy. Otherwise, you do.

Check your implementation of equals(). It should be reflexive, symmetric, and transitive, which is not the case on yours.

It depends on your use-case if you want to deep copy or not. If your elements are immutable you normally not need to do a deep copy. If they can change, it depends if you want to see the changes in your receiver of the copy or not. Typically when you want a snapshot of the given data you will have to deep copy it. However keep in mind that Arrays are most of the time not a good argument or return type for APIs anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top