Questions tagged «static-initialization»

6
最终的定义不正确吗?
首先,一个难题:以下代码显示什么? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long scale(long value) { return X * value; } } 回答: 0 扰流板如下。 如果您打印X的规模(长),并重新定义X = scale(10) + 3,印刷品会X = 0那么X = 3。这意味着X暂时设置为0,后来又设置为3。这是违反final! 静态修饰符与最终修饰符结合使用,还可以定义常量。最后的修饰符指示此字段的值不能更改。 来源:https : //docs.oracle.com/javase/tutorial/java/javaOO/classvars.html [添加了重点] 我的问题:这是一个错误吗?是final不明确的? …

13
Java:静态初始化块什么时候有用?
static块内初始化之间有什么区别: public class staticTest { static String s; static int n; static double d; static { s = "I'm static"; n = 500; d = 4000.0001; } ... 以及单独的静态初始化: public class staticTest { static String s = "I'm static"; static int n = 500; static double d = 4000.0001; ....
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.