How to be cautious to prevent PRIVACY LEAKS when my class has arrayas instance variable?

W

www

Hi,

My class is like following:

public MyClass
{
private double[] _dataArray = new double[100];

public void setDataArray(double[] inputArray) //not good!
{
_dataArray = inputArray;
}

public double[] getDataArray()
{
return _dataArray; //privacy leak
}

}

An Eclipse plugin pointed out that both places could be causing privacy
leak. I know, in Java, array is a tricky data type. It is sort of like
Class variable. So passing array reference is like passing an object
reference: still one object, but with multiple references to it. Each
reference has the ability to modify the object.

What should I do to avoid such a potential problem?

Thank you very much.
 
T

Tom Hawtin

www said:
public MyClass
{
private double[] _dataArray = new double[100];

public void setDataArray(double[] inputArray) //not good!
{
_dataArray = inputArray;
_dataArray = inputArray.clone();
}

public double[] getDataArray()
{
return _dataArray; //privacy leak
return _dataArray.clone();
}

}

An Eclipse plugin pointed out that both places could be causing privacy
leak. [...]

If it was a List<Double> instead of double[], then use new
ArrayList<Double>(data). Collections.unmodifiableList is also a
possibility for outputs, but the unodifiable list tracks the original list.

Tom Hawtin
 
W

www

Tom said:
www said:
public MyClass
{
private double[] _dataArray = new double[100];

public void setDataArray(double[] inputArray) //not good!
{
_dataArray = inputArray;
_dataArray = inputArray.clone();
}

public double[] getDataArray()
{
return _dataArray; //privacy leak return _dataArray.clone();
}

}

I just checked my Java text book. Here is the answer:

public double[] getDataArray()
{
double temp = new double[_dataArray.length];
for(int i=0; i<_dataArray.length; i++)
{
temp = _dataArray;
}

return temp;
}

I am wondering which way is better, comparing to your way (return
_dataArray.clone() ). Is your way a new feature in Java?
 
T

Tom Hawtin

www said:
I just checked my Java text book. Here is the answer:
[...]
double temp = new double[_dataArray.length];
^[]
I am wondering which way is better, comparing to your way (return
_dataArray.clone() ). Is your way a new feature in Java?

No, it's been in there since 1.0. However for 1.4 and earlier you had to
write:

return (double[])_dataArray.clone();

To explicitly write a loop to copy array is nuts.

Tom Hawtin
 
E

Eric Sosman

Tom Hawtin wrote On 04/19/07 15:46,:
www said:
I just checked my Java text book. Here is the answer:
[...]
double temp = new double[_dataArray.length];
^[]


I am wondering which way is better, comparing to your way (return
_dataArray.clone() ). Is your way a new feature in Java?


No, it's been in there since 1.0. However for 1.4 and earlier you had to
write:

return (double[])_dataArray.clone();

To explicitly write a loop to copy array is nuts.

Agreed: In tests discussed on this newsgroup at the
end of February and beginning of March, new-and-loop took
about 30% longer than clone(), and almost twice as long
as new-and-arrayCopy() ...

YMMV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top