Reputation: 408
Since a few months I'm having a Java course and I'm trying to make a simple version of a pokemon game. Until now everything went pretty well but now I'm having troubles.
I have my map with obstacles stored in a class in an Array (private) and use a get method to use the Array in other classes. Somehow these other classes change the Array in the first class.
How is this possible?
Upvotes: 5
Views: 28003
Reputation: 206786
Without seeing your code, it's hard to say what exactly is happening in your program. Maybe something like the following is happening.
Maybe you have a class that has a method that returns a mutable, private member. When you do this, and some other class calls this method, it will be able to change the content of the object that the private member variable refers to. Here is an example:
import java.util.Arrays;
class Example {
// Private member variable
private int[] data = { 1, 2, 3 };
// Method that returns the private member variable
public int[] getData() {
return data;
}
public void showData() {
System.out.println(Arrays.toString(data));
}
}
public class Main {
public static void main(String[] args) {
Example example = new Example();
// Prints: [1, 2, 3]
example.showData();
// Get the array
int[] x = example.getData();
// We can modify the array here!
x[0] = 4;
// Prints: [4, 2, 3]
example.showData();
}
}
This is because objects are arrays, and variables such as data
and x
are references to the same array object - there's only one array and data
and x
both refer to that one array. If you modify the array, you'll see the changes through both variables.
If you want to avoid this, then you shouldn't expose the array by returning it directly in the getData()
method in class Example
. You should make a copy of the array:
public int[] getData() {
// Return a copy of the array
return Arrays.copyOf(data, data.length);
}
Upvotes: 0
Reputation: 5712
private
doesn't mean that you can't change the value of particular thing. when you make a variable private it can't be directly access outside the class. This means that you are no longer able to access a private variable in a class like follows
class A{
private int x = 10;
}
class B{
public void m(){
A a = new A();
a.x = 20; // This is a compile error. Because x is not visible to outside of class A
}
}
Though still you are able to access the variable through a public method. That is what we normally call as encapsulation. e.g.
class A{
private int x = 10;
public int getX(){
return x;
}
public void setX(int val){
x = val;
}
}
class B{
public void m(){
A a = new A();
a.setX(20);
}
}
Upvotes: 3
Reputation: 41
Yes. By using reflection, you can access your private field without giving reference methods.
For example:
Field field = YourClass.class.getDeclaredField("fieldName");
field.setAccessible(true); // Force to access the field
// Set value
field.set(yourClassInstance, "Something");
// Get value
Object value = field.get(yourClassInstance);
Upvotes: 4
Reputation: 1467
Private does not mean it can't be mutated. Private means the reference of that object cannot access directly using the parent object. If you have a getter and which return object reference then any object which has that instance can mutate it.
Upvotes: 7