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!staticclass demand a PApplet's reference. Constructor is the best place for it btW.staticfrom 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
MyNumberinstances declared before I can runMyNumber.randomNo, elsepawill 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
staticfields: O:-)// https://forum.processing.org/two/discussion/21780/static-methods#Item_3 // 2017-Apr-03 final ConstrainedNumber myNum = new ConstrainedNumber(this, 0); void setup() { println(myNum); println(myNum.setLimits(-10, 21).randomNum()); println(myNum); exit(); } static class ConstrainedNumber { final PApplet p; int num, minNum = MIN_INT, maxNum = MAX_INT; ConstrainedNumber(final PApplet pa, final int n) { p = pa; setNum(n); } ConstrainedNumber setLimits(final int low, final int high) { minNum = low; maxNum = high; return this; } int setNum(final int n) { return num = constrain(n, minNum, maxNum); } int randomNum() { return num = (int) p.random(minNum, maxNum); } @Override String toString() { return "num: " + num + "\tlow: " + minNum + "\thigh: " + maxNum; } }Why are you using
staticclasses andstaticmembers? Why not just use inner classes?I wanted to use static
minandmaxto keep track of the smallest and largest values across all the instances ofMyNumber. Meaning to say,minandmaxare 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 theminandmax, 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
ConstrainedNumberso you can setrandomNumin 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 allMyNumberinstances I created.If we
returnthis, 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-
staticas well. ;)That is, all inner classes are nested too. But not all nested classes are inner 1s! :P
Now I understand why you wanted
staticfields as well. :\">Made some tweaks and tests below. Take a look: B-)
// https://forum.processing.org/two/discussion/21780/static-methods#Item_6 // 2017-Apr-04 MyNumber num1, num2; void setup() { MyNumber.p = this; println(MyNumber.report()); println("num1:\t", num1 = new MyNumber(10)); MyNumber.setLimits(-50, 91); println("num2:\t", num2 = new MyNumber(MyNumber.randomNum())); println("num1:\t", num1.setRandomNum()); println("num1:\t", num1); println("num2:\t", num2.setNum(832)); println(MyNumber.report()); exit(); } static class MyNumber { static PApplet p; static int lowest = MAX_INT, highest = MIN_INT; int num; MyNumber(final int n) { setNum(n); } MyNumber setNum(final int n) { lowest = min(num = n, lowest); highest = max(n, highest); return this; } static final void setLimits(final int low, final int high) { lowest = low; highest = high; } static final int randomNum() { return (int) p.random(lowest, highest); } int setRandomNum() { return num = randomNum(); } static final String report() { return "low: " + lowest + "\thigh: " + highest; } @Override String toString() { return "num: " + num + TAB + report(); } }It seems that
random()is the only mathematical function that is not declaredstaticin 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
internalRandomof typejava.util.Randomwhich is used by all methods requiring random numbers. If this was declared as astaticvariable 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 ;)