We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › `private` modifier legally broken
Page Index Toggle Pages: 1
`private` modifier legally broken (Read 2157 times)
`private` modifier legally broken
Mar 27th, 2010, 4:59pm
 
Due to processing use of inner class, private modifier does not actually enforces privacy at all; is there any reason why private is legal? Why not make private modifier causes compilation error instead of making false promise.



As an aside, is there any way to make this class immutable:

class Line {
 private final PVector start;
 private final PVector end;
 // etc...
}

If private had actually worked, we can ensure PVector cannot be modified by instantiating PVector inside Line's constructor and the private modifier guarantee that noone outside our class have access to the actual PVector object. But since private doesn't really do anything, that guarantee does not hold. I can use final .x1, .x2, .x3, .y1, .y2, .y3 instead of PVector; but that means I can't use PVector operations (.cross, .dot, .add, .sub, .mult, etc); not easily at least.
Re: `private` modifier legally broken
Reply #1 - Mar 28th, 2010, 2:27am
 
"[private] is an essential part of Java programming and is not usually used with Processing."

Do you really *need* to use it? I mean, really, really, really need to use it?
Re: `private` modifier legally broken
Reply #2 - Mar 28th, 2010, 4:00am
 
No, I don't. As you see, my personal favorite language of choice is python, for which there is no such thing as access control so I am not really concerned about the lack of privacy itself1.

What I do gets me a bit nervous, is the fact that `private` is legal but does not actually do anything. Even if it's provided for source-level compatibility with existing Java code, shouldn't there be a warning that it does not do what you expect it to do?

1 although a working "private final" modifier would be nice since it guarantees immutability, so I don't even need to think about possibility of accidentally mutating somewhere; however I don't actually mind being courteous and not accessing private data myself (as I would have done in python). The lack of actual privacy is the least of my concern.
Re: `private` modifier legally broken
Reply #3 - Mar 28th, 2010, 5:30pm
 
Quote:
shouldn't there be a warning that it does not do what you expect it to do?


Probably.  Smiley
Re: `private` modifier legally broken
Reply #4 - Mar 29th, 2010, 2:16am
 
private is not working in Processing (at least in .pde files, it works in .java files...) because all classes are inner classes, so have access to the internals of their siblings/parents. There was recently a similar thread on the topic.

Somehow, if you really want it, you can still use the private as it still has a documentation value: it means "don't touch this variable directly". The developer (yourself, in general, in most Processing projects) can still do direct access, but that's at his/her own risk...
Page Index Toggle Pages: 1