-1

Lets say we have these two different constructors. What is the different between the first one and the second one. How is the way to do it? Explain the difference please! (I know you cant have these two constructors in the same class, this is just to show what i mean.

public class StackOverFlow {

private int[] x; // instance variable


StackOverFlow(int[] x) { // constructor
    this.x=x;     
}

StackOverFlow(int[] x) { // constructor

    this.x = new int[x.length];
    for(int k=0 ; k < x.length; k++) {
        this.x[k]=x[k];
    }                  
}         
PrR3
  • 1,250
  • 1
  • 18
  • 26
Karl
  • 57
  • 5
  • you probably just want to add a boolean variable to the constructor to determine whether you want a copy or just store it. – EpicPandaForce Aug 18 '14 at 12:39
  • The first one creates a reference to the first object, the second one creates a new object. So if you change a value in the array that you passed to the first constructor, it will also change the value in the x of the StackOverflow object. If you use the second constructor, the value inside the x of the StackOverflow object will not change just because you change the array you passed to the constructor. – zetches Aug 18 '14 at 12:43

3 Answers3

2

The first constructor assigns a reference of an existing int array to the member variable. The caller of the constructor can later change the array and the change would be reflected in the instance.

The second constructor copies the array, so later changes in the passed array wouldn't change the copy stored in the instance.

int[] intArray = new intArray {1,2,3};

StackOverFlow so1 = new StackOverFlow(intArray); // assume we are using the first constructor 

intArray[1]=5; // changes the array stored in `so1` object

StackOverFlow so2 = new StackOverFlow(intArray); // assume we are using the second constructor

intArray[1]=8; // doesn't change the array stored in `so2` object
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Ok thanks so the second one is obvious the right one, since its not a very good idea to change an object from Another reference.... – Karl Aug 18 '14 at 12:52
  • 1
    @Karl Neither is inherently better or worse than the other - both types have their uses, you just need to make sure you pick the appropriate one for the situation. – JonK Aug 18 '14 at 12:54
  • @Karl I think the second one would usually be the preferred one, though there can always be exceptions. – Eran Aug 18 '14 at 12:58
0

In the first case you tell your instance variable to refer to the given x, so when you change data in one of these variables, that changes also affect the second variable.

And in the second case you create a copy of an object, so your instance variable and variable you pass to constructor will need independent from each other in your further code.

Scadge
  • 9,380
  • 3
  • 30
  • 39
-1

This will not work since you got an ambiguity issue as both constructors receive the same type of parameters. So when you try to create an instance :

StackOverflow instance = new StackOverflow(new int[]{});

There is no way to know which constructor should be called.

You need to decide which behavior is good for you.

I would recommend using the second constructor and create a setter method if you want to set the array from an initialized one :

public class StackOverFlow {

    private int[] x; // instance variable

    StackOverFlow(int[] x) { // conctructor    
        this.x = new int[x.length];
        for(int k=0 ; k < x.length; k++) {
                this.x[k]=x[k];
            }
    }

    public void setTheArray(int[] x) {
        this.x = x;
    }
}
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • 1
    The asker *clearly stated* that they knew it doesn't compile. You've not answered the question at all. – JonK Aug 18 '14 at 12:50
  • 1
    @JonK read the entire answer I explained him why it won't compile and gave him an alternative – giorashc Aug 18 '14 at 12:51
  • 1
    They're not asking for an alternative, they're asking what the differences between the two implementations are. – JonK Aug 18 '14 at 12:51
  • 1
    @giorashc some guys over here don't know where to throw their daily votes – Yehia Awad Aug 18 '14 at 12:52
  • Well he is partly correct as I didn't explain the difference but gave him a solution. But I don't think (and not because its my answer) that it should be downvoted for that. – giorashc Aug 18 '14 at 12:53
  • Downvotes are an essential part of our lives. That is why I don't like Facebook. – filip Aug 18 '14 at 12:57