刚看书, 知道类中static类型的属性是存放在一个叫做全局代码区的地方,而所有的方法也是存在在这里的 ,这个全局代码是区别于栈内存,堆内存的另外一片内存空间么? 另外static类型的方法, 又是存放在哪里呢? 

解决方案 »

  1.   

    哦 上面的说错了 static 类型的属性是存放在 全局数据区的
      

  2.   

    方法不管static与否,都在方法区。
    按照java虚拟机规范第三版(Draft):
    The Java virtual machine has a method area that is shared among all Java virtual
    machine threads. The method area is analogous to the storage area for compiled
    code of a conventional language or analogous to the “text” segment in a UNIX®
    process. It stores per-class structures such as the runtime constant pool, field and
    method data, and the code for methods and constructors, including the special methods
    (§3.9) used in class and instance initialization and interface initialization.
      

  3.   

    举个例子,
    Integer i = new Integer(1000);
    System.out.println(i);
    首先要去方法区找到System.out的类型信息java.io.PrintStream,然后通过类型信息找到println(Object)方法(方法信息和代码)。
    然后加载参数i
    最后调用方法println
    调用方法就是在栈区建立该方法的运行栈,执行println的处理过程。(这个过程中使用i所指向的对象就会和堆产生关联了)