//: interfaces/RandVals.java
package interfaces; /* Added by Eclipse.py */
// Initializing interface fields with
// non-constant initializers.
import java.util.*;
public interface RandVals {
Random RAND = new Random(47);
int RANDOM_INT = RAND.nextInt(10);
long RANDOM_LONG = RAND.nextLong() * 10;
float RANDOM_FLOAT = RAND.nextLong() * 10;
double RANDOM_DOUBLE = RAND.nextDouble() * 10;
} ///:~
//: interfaces/TestRandVals.java
package interfaces; /* Added by Eclipse.py */
import static net.mindview.util.Print.*;
public class TestRandVals {
public static void main(String[] args) {
print(RandVals.RANDOM_INT);
print(RandVals.RANDOM_LONG);
print(RandVals.RANDOM_FLOAT);
print(RandVals.RANDOM_DOUBLE);
}
} /* Output:
8
-32032247016559954
-8.5939291E18
5.779976127815049
*///:~
为什么RandVals接口中的那些变量都是随机产生的,却跟常量一样预先就全部知道了呢?
就算是接口中的变量都是static的,那也会每次初始化的时候都会随机改变一次的吧?
求大神点拨
package interfaces; /* Added by Eclipse.py */
// Initializing interface fields with
// non-constant initializers.
import java.util.*;
public interface RandVals {
Random RAND = new Random(47);
int RANDOM_INT = RAND.nextInt(10);
long RANDOM_LONG = RAND.nextLong() * 10;
float RANDOM_FLOAT = RAND.nextLong() * 10;
double RANDOM_DOUBLE = RAND.nextDouble() * 10;
} ///:~
//: interfaces/TestRandVals.java
package interfaces; /* Added by Eclipse.py */
import static net.mindview.util.Print.*;
public class TestRandVals {
public static void main(String[] args) {
print(RandVals.RANDOM_INT);
print(RandVals.RANDOM_LONG);
print(RandVals.RANDOM_FLOAT);
print(RandVals.RANDOM_DOUBLE);
}
} /* Output:
8
-32032247016559954
-8.5939291E18
5.779976127815049
*///:~
为什么RandVals接口中的那些变量都是随机产生的,却跟常量一样预先就全部知道了呢?
就算是接口中的变量都是static的,那也会每次初始化的时候都会随机改变一次的吧?
求大神点拨