private, public and protected

Hello all, I don't understand how work private in Processing. I thought when something is private you cannot modify directly, but in the example it's not necessary to use an accesser to change the value. It's weird or normal ?

Test t = new Test() ;
void draw() {
  println(t.a++) ;

}

private class Test {
  private int a = 1 ;
  Test() {}
}

Answers

  • edited June 2017 Answer ✓

    It's normal because of a subtlety. When you make a class in Processing, it is actually an inner class of your sketch' main class. The preprocessor turns your entire sketch into a Java class and all classes then become inner/nested. It's a class encapsulated in another class. Note that this is a different thing than inheritance. The private keyword does not prevent the main sketch class to alter a variable of an inner class, since technically they belong to the same class.

    If you put the code for the Test class in a separate file called Test.java, it would make Test a proper class on its own and you would not be able to edit its variables.

  • Thanks a lot for this information ! Damned it, it's sure now for the future I finish to Open Ecclipse to continue to code deep with Processing !

  • edited June 2017

    Is it possible to access to a file.java from Processing ?

    Processing file.pde

    Test t = new Test() ;
    void setup() {
        size(200,200);
    }
    void draw() {
      println(t.get()) ;
    }
    

    java file.java

    class Test {
      private int a = 1 ;
      Test() {}
    
      int get() {
        return a;
      }
    }
    
  • Answer ✓

    Make sure the name of your second file is Test.java

    Kf

  • To create a top-level class in Java create a new tab with the extension.java

    For example a Processing tab called Foo.java would hold the source code for the Foo class.

  • Oups sorry, my mistake the pde file and java file have a same name !

Sign In or Register to comment.