其实对于static方法不用创建实例就已经创建了我不太能理解,那static方法是什么时候创建的呢?程序一开始运行时,所有的static方法和变量都初始化? 例: class mytemp{ static i=0; }public class test { public static void main(String argv[]) {} } 在这段代码中,类mytemp中的i变量是怎么一个生存过程?
但一般来说,所有的类都要被ClassLoader调入,并产生一个对应于该类X的 一个Class Object(这是java,lang.Class的一个实例,不是X的实例) --This is right statement. But it is an Object instance.
[--This is right statement. But it is an Object instance. ]我的理解是这样的: - 这确实是一个Object instance - 但不是X的Object instance (1) - 而是对应于X的java.lang.Class Object instance (2) - 这2个instance 是不同的,(2)是一定要生成的,(1)是不一定要生成的 - 不管有多少个(1),在一个JVM/ClassLoader中,只有一个(2) - 我估计static field/method是位于(2)中的,所以是共享的
Isn't Class inherit from Object?
嘻嘻,这个Object和java.lang.Object混在一起了,我把它改一改,就 不会引起歧义了[--This is right statement. But it is an Object instance. ]我的理解是这样的: - 即使X是一个抽象类,当它被调入ClassLoader后,也要生成一个instance - 但不是X的instance (1) - 而是对应于X的java.lang.Class 的 instance (2) - 这2个instance 是不同的,(2)是一定要生成的,(1)是不一定要生成的 - 不管有多少个(1),在一个JVM/ClassLoader中,只有一个(2) - 我估计static field/method是位于(2)中的,所以是共享的
Then we are on the same boat. In Java everything is rooted from Object, that's the fundamental difference from C++.
from: http://java.sun.com/docs/books/vmspec/ 5.2 Virtual Machine Start-up The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods. In some implementations of the Java virtual machine the initial class could be provided as a command line argument, as in JDK releases 1.0 and 1.1. Alternatively, the initial class could be provided by the implementation. In this case the initial class might set up a class loader that would in turn load an application, as in the Java 2 SDK, Standard Edition, v1.2. Other choices of the initial class are possible so long as they are consistent with the specification given in the previous paragraph.
才真正进入了JVM,进而进入OO的领域(类的导入等),
即main处于底层OS和JVM之间,在这里,main被规定为static,
可能是利用了在一个class物理存储(如一个文件,或位于数据裤中)
中static方法的位置,从而定位main方法作为入口如果你用reflection的方法来“运行”一个类,你同样可以从main
作为入口,这时main就不一定非要是static的了上面第一条是瞎猜的,感兴趣可以看JVM spec, 嘻嘻
--Are u sure? 不需要先生成一个类实例的, I don't agree on this statement...
一个抽象类(不可实例化)中的一个static方法但一般来说,所有的类都要被ClassLoader调入,并产生一个对应于该类X的
一个Class Object(这是java,lang.Class的一个实例,不是X的实例)
例:
class mytemp{
static i=0;
}public class test
{
public static void main(String argv[])
{}
}
在这段代码中,类mytemp中的i变量是怎么一个生存过程?
一个Class Object(这是java,lang.Class的一个实例,不是X的实例)
--This is right statement. But it is an Object instance.
static成员的初始化是在JVM需要使用一个类时进行的,而不是生成一个该类的实例时。
- 这确实是一个Object instance
- 但不是X的Object instance (1)
- 而是对应于X的java.lang.Class Object instance (2)
- 这2个instance 是不同的,(2)是一定要生成的,(1)是不一定要生成的
- 不管有多少个(1),在一个JVM/ClassLoader中,只有一个(2)
- 我估计static field/method是位于(2)中的,所以是共享的
不会引起歧义了[--This is right statement. But it is an Object instance. ]我的理解是这样的:
- 即使X是一个抽象类,当它被调入ClassLoader后,也要生成一个instance
- 但不是X的instance (1)
- 而是对应于X的java.lang.Class 的 instance (2)
- 这2个instance 是不同的,(2)是一定要生成的,(1)是不一定要生成的
- 不管有多少个(1),在一个JVM/ClassLoader中,只有一个(2)
- 我估计static field/method是位于(2)中的,所以是共享的
这个区域的成员一旦被分配,就不再改变地址啦。直到程序结束才释放。
main()就存储在这里。
尽管包含main()的类还没有被实例化,但是main()方法已经可以使用啦。
而且JVM将会自动调用这个方法。通过main()的调用,再实例化其他的对象,
也包括自己所在的类。
5.2 Virtual Machine Start-up
The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.
In some implementations of the Java virtual machine the initial class could be provided as a command line argument, as in JDK releases 1.0 and 1.1. Alternatively, the initial class could be provided by the implementation. In this case the initial class might set up a class loader that would in turn load an application, as in the Java 2 SDK, Standard Edition, v1.2. Other choices of the initial class are possible so long as they are consistent with the specification given in the previous paragraph.