【发布时间】:2016-01-11 13:39:18
【问题描述】:
假设我有一个带有一些“Utils”类的 Java 项目,并且这些类只有 static 方法和成员。
一旦我运行我的应用程序,这些方法和成员是否会自动加载到内存中?还是只有在我按照代码调用类时才会发生这种情况?
编辑:一些示例代码来说明我的问题。
RandomUtils.java
public class RandomUtils {
private static Random rand = new Random();
public static int randInt(int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
return rand.nextInt((max - min) + 1) + min;
}
}
MainClass.java
public class MainClass {
public static void main(String[] args) {
// Some other operations. Is my class already loaded here?
int randomNumber = RandomUtils.randInt(1,10); // Or is it only loaded here?
}
}
如果该类有其他静态成员和方法,并且如果它仅在我调用其中一个时加载,那么其他方法也会被加载?
【问题讨论】:
-
投反对票的人,你能解释一下是什么问题吗?
标签: java static static-methods