final class的loading问题~

seraphmk2 2002-02-09 02:40:50
在thinking in java里第6章关于final class里有这样一题:
Prove that class loading takes place only once. Prove that loading may be caused by either the creation of the first instance of that class, or the access of a static member.

该如何证明啊??我不懂啊~~loading过程是如何体现的呢??是不是新建一个对象,然后用构建器中的显示语句来实现啊?我那样做却显示了多个啊..汗..还有那个访问静态成员该如何做啊??下面是我编的代码...显示的不对啊...汗...

final class Amphibian{
static int i=1;
Amphibian(){
System.out.println("constructor");
}
static void ptr(){
System.out.println("ok");
}
}


public class myjava{
public static void main(String[] args){

System.out.println(i);

Amphibian f2= new Amphibian();
Amphibian f1= new Amphibian();
}
}

...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
seraphmk2 2002-02-11
  • 打赏
  • 举报
回复
up^^
seraphmk2 2002-02-10
  • 打赏
  • 举报
回复
我这里的code不对……我不知道该如何编才对,不知道如何证明……
sojkey 2002-02-09
  • 打赏
  • 举报
回复
只有static的域才是所有实例共用的,系统只分配一个存储空间。空间在初次实例化该类或是初次直接调用该类的静态域(或静态方法)时分配。例如在上例中直接调用Amphibian.i或Amphibian.ptr()
seraphmk2 2002-02-09
  • 打赏
  • 举报
回复
那这里应该如何来证明呢??
Luke_cn 2002-02-09
  • 打赏
  • 举报
回复
我想这里的loading是指"在内存中开辟一个区域来存放对象"

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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