来说说实例方法是不是会被安排在方法区

dracularking 2010-07-09 11:06:57
来说说实例方法是不是会被安排在方法区,是的话什么形态,什么运作方式
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zangxt 的回复:]

方法是不需要复制的。应该就是在方法区。
可以认为类型信息中保存一些函数指针,调用时根据这些指针去读取实际代码。
具体实现可以类似与C++里的virtual table。
[/Quote]
嗯,这样设计可以,再验证下,加载时类型信息都是打包存储在方法区
Inside a Java Virtual Machine instance, information about loaded types is stored in a logical area of memory called the method area.

在深入虚拟机第五章
Type Information

For each type it loads, a Java Virtual Machine must store the following kinds of information in the method area:

* The fully qualified name of the type
* The fully qualified name of the typeís direct superclass (unless the type is an interface or class java.lang.Object, neither of which have a superclass)
* Whether or not the type is a class or an interface
* The typeís modifiers ( some subset of` public, abstract, final)
* An ordered list of the fully qualified names of any direct superinterfaces

Inside the Java class file and Java Virtual Machine, type names are always stored as fully qualified names. In Java source code, a fully qualified name is the name of a typeís package, plus a dot, plus the typeís simple name. For example, the fully qualified name of class Object in package java.lang is java.lang.Object. In class files, the dots are replaced by slashes, as in java/lang/Object. In the method area, fully qualified names can be represented in whatever form and data structures a designer chooses.

In addition to the basic type information listed above, the virtual machine must also store for each loaded type:

* The constant pool for the type
* Field information
* Method information
* All class (static) variables declared in the type, except constants
* A reference to class ClassLoader
* A reference to class Class

那么基本类型信息以外的方法信息到底是实体还是指针呢,但是对方法而言,可以有类似的东东吗 按理也没有理由不可以,只是在reference以外没有见过
ZangXT 2010-07-15
  • 打赏
  • 举报
回复
方法是不需要复制的。应该就是在方法区。
可以认为类型信息中保存一些函数指针,调用时根据这些指针去读取实际代码。
具体实现可以类似与C++里的virtual table。
dracularking 2010-07-09
  • 打赏
  • 举报
回复
嘿嘿,深入虚拟机一个人看这书无聊且缺乏动劲,所以大家一起讨论下
从虚拟机层次深入是很有必要的,继续挖掘

按我个人的想法,如果对于某个只是从父类继承而来的实例方法供子类使用,它会不会在继承时拷贝一份给子类,应该不会,因为方法仅仅是对象的动作,而不是状态,对于动作,父类子类是一致的,所以没必要重复,但是放在哪里,既然不是属于对象本身,那要不要放在一个第三方区域比如方法区...
ZangXT 2010-07-09
  • 打赏
  • 举报
回复
先mark,回头再回,最喜欢这类看似无聊的问题,呵呵
dracularking 2010-07-09
  • 打赏
  • 举报
回复
有理有据固可,但更欢迎自己的想象和推理附上一定理由

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧