请从而向对象的角度来解释
--和 "而向对象" 没太大关系,与 JVM 规范有关。JVM 需要定位程序的入口,必须找到一个众所周知的入口。不要太钻牛角了

解决方案 »

  1.   

    具体不太清楚,我个人意见和楼上相同,以下是我的猜测:在这里Java延袭了C的影子,我估计其实是在main之后,
    才真正进入了JVM,进而进入OO的领域(类的导入等),
    即main处于底层OS和JVM之间,在这里,main被规定为static,
    可能是利用了在一个class物理存储(如一个文件,或位于数据裤中)
    中static方法的位置,从而定位main方法作为入口如果你用reflection的方法来“运行”一个类,你同样可以从main
    作为入口,这时main就不一定非要是static的了上面第一条是瞎猜的,感兴趣可以看JVM spec, 嘻嘻
      

  2.   

    之所以需要是static的是因为只有类的static方法是可以不需要先生成一个类实例的。而java的全类封装的特性使得没有游离于类外的方法,JVM想启动程序就必须有一个入口点,这个点就是封装于一个public类的static方法,而main(String argv[])只是人为规定的一个规范而已。
      

  3.   

    因为只有类的static方法是可以不需要先生成一个类实例的
    --Are u sure? 不需要先生成一个类实例的, I don't agree on this statement...
      

  4.   

    调用类X的static方法,是不需要生成X的一个instance的,比如你可以调用
    一个抽象类(不可实例化)中的一个static方法但一般来说,所有的类都要被ClassLoader调入,并产生一个对应于该类X的
    一个Class Object(这是java,lang.Class的一个实例,不是X的实例)
      

  5.   

    其实对于static方法不用创建实例就已经创建了我不太能理解,那static方法是什么时候创建的呢?程序一开始运行时,所有的static方法和变量都初始化?
    例:
       class mytemp{
         static i=0;
    }public class test
    {
      public static void main(String argv[])
    {}
    }
    在这段代码中,类mytemp中的i变量是怎么一个生存过程?
      

  6.   

    但一般来说,所有的类都要被ClassLoader调入,并产生一个对应于该类X的
    一个Class Object(这是java,lang.Class的一个实例,不是X的实例)
    --This is right statement. But it is an Object instance.
      

  7.   

    具体的内部细节我不清楚,但是从一个类的所有的实例都共享一份static成员来看,这个static成员是由JVM或者其它什么不和类实例直接相关的机制来管理的。如果仅仅因为类加载器调入就说它产生一个实例,这好像说不通。要知道java的工作机制是先由类加载器加载所有需要的类,然后进行字节码验证,验证通过后才交给解释器解释执行的,难道为了验证一个类就需要生成它的实例吗?
    static成员的初始化是在JVM需要使用一个类时进行的,而不是生成一个该类的实例时。
      

  8.   

    [--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)中的,所以是共享的
      

  9.   

    Isn't Class inherit from Object?
      

  10.   

    嘻嘻,这个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)中的,所以是共享的 
      

  11.   

    Then we are on the same boat. In Java everything is rooted from Object, that's the fundamental difference from C++. 
      

  12.   

    to : simoncn(早睡早起精神好) “JVM 规范”哪里有?想看看!
      

  13.   

    所有static成员都是在程序装载时初始化的,被分配在一块静态存储区域。
    这个区域的成员一旦被分配,就不再改变地址啦。直到程序结束才释放。
    main()就存储在这里。
    尽管包含main()的类还没有被实例化,但是main()方法已经可以使用啦。
    而且JVM将会自动调用这个方法。通过main()的调用,再实例化其他的对象,
    也包括自己所在的类。
      

  14.   

    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.