Reputation: 13
I code on Processing for a few months now and I'm trying to use java classes. I'm a newbie on java classes and I believe that private attributes of a class cannot be modified outside this class.
However I did change a private attribute of an object in Processing's setup()
void. I don't understand why I am able to do that.
Can anybody help me ?
A part of the class code:
public class Character {
private String name;
...
public Character(String pName, ...) {
name = pName;
...
public void dname() {
println(this.name);
}
}
The setup()
code:
void setup() {
player = new Character("John Doe", ...);
player.dname(); //I get "John Doe".
player.name = "tara";
player.dname(); //I get "tara", without any error.
}
Thank you very much !
Upvotes: 1
Views: 854
Reputation: 42176
If this is in the Processing editor, then Java classes are inner classes behind the scenes.
This is why you can access private variables in the class from your sketch. This is also why you can access Processing functions from inside your class.
You could try putting the class in its own tab. Make sure the tab ends with .java
, so it's treated as a "real" Java class.
But Processing tends to hide stuff like access modifiers from you, so you might be best off just not worrying about it too much.
Upvotes: 2