Access/visibility question

edited January 2017 in Questions about Code

Hello all,

I really like the way Processing 3 gives the ability to use graphics in Java and I know it hides some of the more quirky aspects of Java (off the top of my head : including libraries, declaring main), I wonder if the problem I am having is related to complexity hiding. I have a main sketch file :

void setup () {
  size(500, 500);
}

void draw() {
  noLoop();
  TestClass tcObj = new TestClass();
  println(tcObj.a); // Illegal in Java
  tcObj.b=99999;    // ditto
  println(tcObj.b); // ditto
  exit();
}

that, as you can see, makes an instance of a TestClass (which is defined in another file) :

public final class TestClass
{
    private int a;
    private int b;

   /** Constructor. */
   public TestClass()
   {
      a=1;
      b=2;
   }
}

In the sketch it allows me to view and redefine the contents of another classes' private variable, something not permitted in Java.

I see from the exported .java file that all the code seems to be defined within an inner class (I think is the right term) of of the sketch class, is that the problem; effectively making all code and state visible everywhere? If so is there any solution? It just seems that it allows so much dangerous or sloppy code (and I do enough of that on my own).

Hmm, just found this :

https://processing.org/reference/environment/#Tabs

Does that mean its time to give in and use the Eclipse IDE and write the java code without the help of the Processing IDE?

Tagged:

Answers

  • You're correct. Classes created with the Processing IDE become inner (nested) classes. This means other classes can easily use the graphic methods and variables from the main class.

    To force Processing to compile TestClass as a different class and not a nested class, simply rename the tab to TestClass.java in the IDE. All non-nested classes need to be in their own tab with the .java extension.

  • edited January 2017

    Classes created with the Processing IDE become inner (nested) classes.

    Just a pedantic clarification: Although all inner classes are nested, not all nested classes are inner. @-)
    That is, nested classes can be declared w/ keyword static and behave very similar to a top class.
    Actually all top classes are implicitly static in Java. :-B

  • Moving to eclipse isn't an easy solution - you end up having to pass the top class around a lot because a lot of things need it and no longer have access to it by being inner classes. All calls to standard functions have to be made via a reference to this class, ie p.line(...); and it gets tedious quickly.

  • without the help of the Processing IDE?

    The Processing ide can get terrible for large projects. That said, eclipse has decent tools of its own. Code completion and syntax highlights are fine etc. What you lose is being able to run quickly with Ctrl-r. And things like color (which is a preprocessor trick). Swings and roundabouts.

  • @GoToLoop You say

    Actually all top classes are implicitly static in Java.

    But I don't understand. I thought there is no such thing as a top level "static" class?

  • edited January 2017
    • It's true Java doesn't allow us to redundantly & explicitly declare a top class as static.
    • Definition of static in Java is anything that can be reached w/o instantiating something else 1st.
    • As long a class/interface has been imported, we can target it w/o any intermediary.
    • For nested classes, w/o keyword static, they're called inner classes.
    • In order to reach an inner class, we need to instantiate its enclosing class 1st.
    • However, if it's declared as static, we can directly reach it w/o an instance of its enclosing class.
    • Interfaces are always static, even when they're nested.
  • The Processing ide can get terrible for large projects. That said, eclipse has decent tools of its own. Code completion and syntax highlights are fine etc. What you lose is being able to run quickly with Ctrl-r. And things like color (which is a preprocessor trick). Swings and roundabouts.

    I went with IntelliJ and apart from a very short episode never used Eclipse so I can't compare. In IntelliJ I can hit shift+F9 to debug then I can just do ctrl+F5 to restart the sketch. It even supports live hotswapping of updated classes so you can leave the sketch running and change something in draw() and have it appear without restarting. Doesn't always work but enough times to be useful. I imported the Processing source code and the source of all libraries and then I can just ctrl+click on any class or method to jump to its implementation. Without source code you get a decompiled version. Also, ctrl+shift+enter to close all brackets, add a semicolon and jump to the next line automatically. It's just so much faster... Takes a while to get used to but it's really worthwhile. It's slower to get a new sketch started as you need to manually browse to the Processing .jars and the library .jars but there's probably some way to make a template from that. There's ton of other features like Git, autocomplete, smart suggestions, and so on. It actively looks for ways to improve your code, which is cool.

    So in conclusion don't be scared to make the jump to Eclipse or IntelliJ.

    • It's true Java doesn't allow us to redundantly & explicitly declare a top class as static. (...)

    Thanks for that explanation, TIL.

    • It's true Java doesn't allow us to redundantly & explicitly declare a top class as static.
    • Definition of static in Java is anything that can be reached w/o instantiating something else 1st.
    • As long a class/interface has been imported, we can target it w/o any intermediary.

    Thanks for the explanation. Very clear now.
    It seems most tutorials and textbooks don't give a proper meaning for static.

  • edited January 2017

    static isn't OOP. Their members are shared globally w/o instantiating their class. >-)

    And nested classes are practically unknown in the world of Java programming. (:|

  • You're probably right - I have never seen a static nested class, but I've seen many non-static ones.

Sign In or Register to comment.