A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2013-4-19 20:56  /  2009 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes...

  2.         A frame is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions.

  3.         Frames are allocated from the Java Virtual Machine stack of the thread creating the frame. Each frame has its own array of local variables , its own operand stack , and a reference to the run-time constant pool of the class of the current method.

  4.         In some of Oracle's implementations of the Java Virtual Machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data.
  5. ~The JVM doc
复制代码
你自己猜测的其实是不正确的.
局部变量是必须加final的,而数据成员则不必要.
至于为何必须加final,这与java语言自身有关系,java本身就是有某些缺陷的,它也在不断改进自己半吊子的窘境.

不过,您是有富有想象力的人!

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
Neverbelazy 发表于 2013-4-19 23:30
谢谢你的意见,不过有一点我还是不太理解:

被final修饰的类如果是常量的话,生命周期最长的话,它是怎 ...
  1. class Test{
  2.         final int x = 1;
  3. }
  4. public class Get {
  5.         public static void main(String[] args)
  6.                 throws Exception{
  7.                 System.out.println(Test.class.getDeclaredField("x").getInt(new Test()));
  8.         }
  9. }
复制代码
被final修饰的量并不能称之为生命周期最长的,最长的应是被static修饰的量.
回复 使用道具 举报
Neverbelazy 发表于 2013-4-19 23:34
谢过回复。。。不过上面的引用 JVM doc好像也没解释我的问题啊???

试试过一两个月再看我摘录的这段话.
回复 使用道具 举报
本帖最后由 杜鹏飞 于 2013-4-20 18:21 编辑
Neverbelazy 发表于 2013-4-20 13:35
ok 这种情况确实是,虽然我没见过这段代码,不过可以看出在这段代码还是 new 了一个 Test()类才实现的调用 ...

如果变量被定义在局部,且被final修饰,那么我们只能获取这个变量的值.
至于y,只是一个引用,编译器不会为其创建实例.

比如下面这段代码
  1. void f(const int& i){
  2. }

  3. int main(){
  4.         int i = 1;
  5.         f(i);
  6. }
复制代码
  1. 0x00401503 <+14>:        movl   $0x1,0x1c(%esp)
  2. 0x0040150b <+22>:        lea    0x1c(%esp),%eax
  3. 0x0040150f <+26>:        mov    %eax,(%esp)
  4. 0x00401512 <+29>:        call   0x4014f0 <f(int const&)>
复制代码
回复 使用道具 举报
Neverbelazy 发表于 2013-4-20 13:38
ok 我先看完java基础课程,再有时间研习下 JVM的机制。

jvm的机制我也不太了解,我都是需要甚么再去查看文档.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马