Class Method and Private Method

Big difference between Math and Random is static and non-static methods. Random::nextInt is a method on an instance of the Random class, and has to be called on an instance. Math::sqrt is a static method, basically a method on the class rather than an instance, so does not have to be called via an instance of Math (which you can’t create anyway).

Similarly, the normal Processing random() method is a method on an instance of PApplet. You can call it in a sketch without calling it on an instance of PApplet only because your code is an instance (of a subclass) of PApplet. As soon as you try and call such a method from another static context, as here, you need to have the instance of PApplet to call it on. Note that @quark code is nothing like what you’re describing - they are passing in the instance of PApplet in the init() method.

Despite what you say earlier, what you’re describing isn’t possible in Java either - what you have working is something quite different!

No, Math is part of the java.lang package, all of which is imported by default. Processing also adds some extra default imports.

You can also import static methods - eg. import static java.lang.Math.*; and just call sqrt(). This will likely conflict with a lot of Processing methods if you do it in a sketch though.

2 Likes