Hi everybody, Im trying to use some class method that I create but it is not work cause of the error: The method “method_name” cannot be declared static; static methods can only be declared in a static or top level type.
My intention is use the method as used in Java. Full example so everybody understand
Main.pde
void setup()
{}
void draw()
{
int a = Example.sum(5, 4); //This is the way that I want to use
Example foo = new Example();
//Useless method just created to let you understand difference between this and the above one!
foo.do(5);
}
Example.pde
class Example()
{
//some variables
//some contructor
void do(int param)
{
this.variables += param;//sum variables with parameters passed
}
public static int sum(int a, int b)
{
return a*b;
}
}
Hopefully I had been clear!
So this is the way I used to create a class method in Java so I can call it whenever I want pass my params and get the result but using processing Im not able, there is a way?
Thank you
In Processing the sketch code is wrapped up as a top level class so any class declared inside it will be an inner class the easy solution here is to declare the class static like this
Yeah sorry forgot it there!!!
Anyway I tried do that stuff but I get this error:
Cannot make a static reference to the non-static method random(float, float) from the type PApplet
I see that I have 3 random() functions and when I comment that the error goes to the next one!
If I comment out all random function the class works! But I need that random function
Well, Im doing a Matrix class, maybe you already have one to share with static method
This is simply not possible. You can’t call an instance method from a static context - what instance would you be calling the method on?! Unless you pass in the PApplet as an argument. Why do you want this to be static anyway?
Cause I dont know any other method to do that, the previous code was broken due to this and I had to adapt adding more lines!
Anyway, if it isn’t still possible I will wait for an update!
In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>
That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.
Extra info:
Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.
It has been explained why this is the case. Waiting for something to change that is not going to change is pointless! Try reading through again and understanding why this is happening, and why the bug is in your code!
The following sketch code does what you want. It is obvious that you don’t understand the code so I suggest that you do some basic research on object orientated programming (OO) and start with classes and objects
static class Example
{
static PApplet papp;
public static void init(PApplet papplet) {
papp = papplet;
}
public static int sum(int a, int b) {
return a+b;
}
public static float rand(float max) {
return papp.random(max);
}
}
I introduce your adjustments and now the method random can only have 1 params!
EDIT:
All fixed, I see that I can import whatever I want from java libraries so I imported utils.Random and initialize a new Random() var!
And now I finally use instance method Thank you quark for let me deep more!
Anyway I have a little question, what difference between Math and Random?
Becaue I can call Math.sqrt() without import nothing and when I call Random.nextInt() I cant!
Is this problem due to static non static class?
Reading between docs I saw that MAth is Public Final class, instead Random is just public class. IS this the main difference?
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.