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 › Access modifiers
Page Index Toggle Pages: 1
Access modifiers (Read 807 times)
Access modifiers
Mar 15th, 2010, 3:26pm
 
Hi all,

I'm quite new to Java and Processing, having come from a hobbyist C++ background.  Could anyone tell me why the Processing code below works, having accessed happily twice what I would hope is an inaccessible property?

Many thanks in advance.

Code:
Test test;

void setup ()
{
 test = new Test ();
 test._privates = "well fondled";
 println ("_privates = " + test._privates);
}

void draw ()
{
}

private class Test
{
 private String _privates;
 
 Test ()
 {
   _privates = "virginal";
   println ("_privates = " + _privates);
 }
}


Sample output:

Code:
_privates = virginal
_privates = well fondled

Re: Access modifiers
Reply #1 - Mar 15th, 2010, 5:07pm
 
"[private] is an essential part of Java programming and is not usually used with Processing. Consult a Java language reference or tutorial for more information."
Re: Access modifiers
Reply #2 - Mar 15th, 2010, 5:58pm
 
TfGuy44 wrote on Mar 15th, 2010, 5:07pm:
"[private] is an essential part of Java programming and is not usually used with Processing. Consult a Java language reference or tutorial for more information."

Well, this is true and someone had pointed me to it.  But the wording suggests infrequent usage, rather than a total lack of implementation.

Can anyone clarify
Re: Access modifiers
Reply #3 - Mar 15th, 2010, 6:12pm
 
I think it has to do with the fact that classes in Processing are all "inner" classes - that is, contained within the main (PApplet) class. So even if you make things "private" they are still inside the main class, and therefore part of it, and not private to that class. I think you can make a "proper" class if you create a new tab and give it a name ending in .java... but then you have to pass in the PApplet to use the Processing-specific stuff - gets much more complicated.
Re: Access modifiers
Reply #4 - Mar 15th, 2010, 6:17pm
 
See the last section in here for more information:

http://processing.org/learning/eclipse/
Re: Access modifiers
Reply #5 - Mar 16th, 2010, 4:55am
 
Giles wrote on Mar 15th, 2010, 6:12pm:
I think it has to do with the fact that classes in Processing are all "inner" classes - that is, contained within the main (PApplet) class.

Yes.
Privacy is harder to maintain when you live under the same roof... Smiley
Re: Access modifiers
Reply #6 - Mar 16th, 2010, 7:42am
 
PhiLho  wrote on Mar 16th, 2010, 4:55am:
Giles wrote on Mar 15th, 2010, 6:12pm:
I think it has to do with the fact that classes in Processing are all "inner" classes - that is, contained within the main (PApplet) class.

Yes.
Privacy is harder to maintain when you live under the same roof... Smiley


Thanks to you both for the clarification!  I was under the false impression that declaring the class itself private would allow it some modesty.  This is a shame -- I want to demonstrate and justify encapsulation to some students new to programming without having to pull them out of the Processing bubble.
Page Index Toggle Pages: 1