We are about to switch to a new forum software. Until then we have removed the registration on this forum.
When and when isn't it good to have direct access to public variables.
For example I have:
`PackerNode root;`
And i want access to it, i could make it public, but i also could:
public PackerNode getRoot() { return root } ;
What do you guys prefer? And how do you guys make decisions?
Answers
Is it about creating a Processing's library? :-??
For data structures or something more specialized I'd use
protected
.B/c that would open up a way for tweaking via inheritance! :ar!
For more generic fields like x, y, w, h, etc., and also for constants, I'd set them
public
.That's what Processing's PVector and Java's Point classes do, for example! (*)
yes it's for a library. Till when is is it generic? :)
If you make a field public then because it is accessible anywhere it can be changed by the user in a way the developer did not expect with possibly disasterous effect. If this is likely to be a problem then go for a private or protected attribute and use a public getter.
Apart from public/protected/private access specifiers their is a fourth one which is used if none of the others are specified - package access level.
So in the statement
The attribute
node
is directly accessible in its own class and by any class in the same package. This is useful in a library because the attribute is easily accessible to the library classes but not available to the library user.Just another point re PDE
I am not sure about Processing I suspect (but I am not sure) that the preprocessor will add the public access specifier to any attribute if an access specifier is missing. Package level access still works in java tabs because they are unaltered by the preprocessor, and they obviously work in Eclipse or Netbeans.
thanks once again.