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 442 times)
access modifiers?
Dec 18th, 2008, 9:38am
 
Code:

A a;

void setup() {
a = new A();
println(a.b);
}

class A{
private int b;

A(){
b = 5;
}
}


does not give an error.

Does Processing not support access modifiers? Is this a bug?
Re: access modifiers?
Reply #1 - Dec 18th, 2008, 12:27pm
 
Everything in processing gets wrapped into another class, so your class A is actually an inner class, and I think because of that the main class has access to even private things.
Re: access modifiers?
Reply #2 - Dec 19th, 2008, 10:43pm
 
even

Code:

A a;
B b;

void setup() {
a = new A();
println(a.val);
b = new B();
}

class A{
private int val;

A(){
val = 5;
println(val);
}
}

class B{

B(){
print(a.val);
}
}


does not cause a problem. So as far as I can tell access modifies to nothing in processing.
Re: access modifiers?
Reply #3 - Dec 19th, 2008, 11:44pm
 
Not too sure about how access is managed in inner classes, I don't feel like reading the whole Classes chapter...
But what you show is standard Java behavior:
Code:
class Test
{
 A a;
 B b;

 public static void main(String[] args)
 {
   Test tdj = new Test();
   tdj.init();
 }

 void init()
 {
   a = new A();
   System.out.println(a.val);
   b = new B();
 }

class A {
 private int val;

 A() {
   val = 5;
   System.out.println(val);
 }
}

class B {

 B() {
  System.out.println(a.val);
 }
}
}
compiles and runs fine in DrJava.
Re: access modifiers?
Reply #4 - Dec 20th, 2008, 12:54am
 
oook...

i was not familiar with nested classes. reading...
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
Re: access modifiers?
Reply #5 - Dec 20th, 2008, 12:56am
 
this explains it:


A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
Re: access modifiers?
Reply #6 - Dec 20th, 2008, 11:14am
 
Ah, I missed it somehow. The key is indeed "Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.", which can be interpreted as: they have access to private fields of other inner classes in the same parent class.
I wouldn't dare to interpret it so far, but experiments show it is the case.
Of course, while it is good to know for expanding our knowledge, I think this is rarely used in practice (using getters for example, or other access levels like default one (package protected)).
Page Index Toggle Pages: 1