We are about to switch to a new forum software. Until then we have removed the registration on this forum.
static class MyNumber{
int no = 0;
static int max= -9999999;
static int min = 9999999;
MyNumber(int n){
no = n;
if (no>max){ max = no; }
if (no<min){min = no; }
}
static void report(){
println("Min: "+min+", Max: "+max);
}
static float randomNo(){
return random(min, max);
}
In the above code, I get the error Cannot make a static reference to the non-static method random(float, float) from the type PApplet
. Why is that so? I wish to keep a list of min and max numbers for all instances of MyNumber
, and hope to operate on those values via a static method.
Answers
static
!static
class demand a PApplet's reference. Constructor is the best place for it btW.static
from your class, turning it a PApplet's inner class.https://Processing.org/reference/random_.html
http://Docs.Oracle.com/javase/8/docs/api/java/lang/Math.html#random--
http://Docs.Oracle.com/javase/8/docs/api/java/util/Random.html
I attempted method 1:
Works well except I need to have at least one
MyNumber
instances declared before I can runMyNumber.randomNo
, elsepa
will not be initalised and I will get a NPE.I doubt I will run into a situation where I will call a static method without initialising any instances, but I shall just ask for learning purposes: Is there a way around that?
Can you elaborate? I'm clueless what that means. Does it mean I extend PApplet?
I've meant about not declaring your class as
static
.Given your field pa is
static
, you can initialize it directly within setup():MyNumber.pa = this;
However, I don't get why you're declaring your fields
static
.Doing so you can't individualize instances of your class MyNumber.
They'll all share fields min, max & pa. Only field no is created for each instance of it. :-@
Just did another similar class demoing how to do it w/o
static
fields: O:-)Why are you using
static
classes andstatic
members? Why not just use inner classes?I wanted to use static
min
andmax
to keep track of the smallest and largest values across all the instances ofMyNumber
. Meaning to say,min
andmax
are not set by the user, they record the smallest and largest number among all instances of MyNumber. I was also hoping to be able to do some operations like generate a random number from among themin
andmax
, without making an instance ofMyNumber
.I will be building up from this class and integrate it into my battery data class where I want to keep track of the min and max values across each batteries to draw the graph.
Interesting to see how you return an instance of
ConstrainedNumber
so you can setrandomNum
in the same line.I am not familiar with inner classes in this sense. From I know, inner classes are classes within a class. I guess since all classes in Processing are part of one instances of PApplet, just do not use
static
? I was hoping to use static because I want min and max to be shared among all instances ofMyNumber
, so I can know the smallest and largest number among allMyNumber
instances I created.If we
return
this, i.e. the same object which invoked the method, we turn it into a chainable method.Actually Java's stricter name for such is nested class. ~O)
Inner class is a more specific name for when the nested class is non-
static
as well. ;)That is, all inner classes are nested too. But not all nested classes are inner 1s! :P
Now I understand why you wanted
static
fields as well. :\">Made some tweaks and tests below. Take a look: B-)
It seems that
random()
is the only mathematical function that is not declaredstatic
in PApplet how ridiculus.This is a simple modification of your class avoiding the need for an instance of PApplet to get a random number.
In order to keep a randomSeed(): https://Processing.org/reference/randomSeed_.html
PApplet::random() gotta be non-
static
, keeping an instance of Random as well! :>There's no shame on that. Don't be too prejudice about it. :-\"
PApplet creates an instance variable of called
internalRandom
of typejava.util.Random
which is used by all methods requiring random numbers. If this was declared as astatic
variable and then all these methods includingrandomSeed()
could also bestatic
.Perhaps 'rediculus' was overstating it a bit :D , but I don't see a problem with having these as static methods in the same way as all the other mathematical methods are static e.g. min, max, sin, cos, tan, abs, map, constrain ... etc etc etc.
There maybe a reason but I cannot think of a good one.
That'd be very bad for PApplet threaded multi-windows. [-X
B/c all instances of PApplet would have 1 shareable "seed" only! :-@
The latest PApplet instance to call randomSeed() would determine the "seed" for all the others! :O)
Even though Random is thread-safe, concurrent calls to the same instance of it would cause contention and consequent poor performance: :-SS
http://Docs.Oracle.com/javase/8/docs/api/java/util/Random.html
OK good point, I was not aware of that, but I promise to remember it for the next time I use PApplet threaded windows ;)