We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
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.
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. :-BMoving 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.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
But I don't understand. I thought there is no such thing as a top level "static" class?
static
.static
in Java is anything that can be reached w/o instantiating something else 1st.static
, they're called inner classes.static
, we can directly reach it w/o an instance of its enclosing class.static
, even when they're nested.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.
Thanks for that explanation, TIL.
Thanks for the explanation. Very clear now.
It seems most tutorials and textbooks don't give a proper meaning for
static
.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.