一道SCJP题目求解

sha256cn 2002-03-12 02:26:06
class a{
public int avar;
public a(){
System.out.println("AAA");
dosomething();
}
public void dosomething(){
avar=1111;
System.out.println("A.dosomething()");
}
}
class b extends a{
public int bvar=2222;
public b(){
System.out.println("BBBB");
dosomething();
System.out.println("Avar="+avar);
}
public void dosomething(){
System.out.println("Bvar="+bvar);
}
public static void main(String[] args){
new b();
}
}

OUTPUT:
AAAA
Bvar=0 //为什么这里Bvar为0而下面Bvar为2222呢?
BBBB
Bvar=2222
Avar=0

望各位高手赐教,THX
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
duodonglai 2002-03-13
  • 打赏
  • 举报
回复

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden. If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§2.5.1).

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:


1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 5.

3. If this constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this) and is in a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

4. Execute the instance variable initializers for this class, assigning their values to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type. This was a bug.)

5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

上一个忘加序号了,不好意思。
《java虚拟机规范》
duodonglai 2002-03-13
  • 打赏
  • 举报
回复
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden. If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§2.5.1).

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:


Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 5.

If this constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this) and is in a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

Execute the instance variable initializers for this class, assigning their values to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type. This was a bug.)

Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

《java虚拟机规范》
micheas 2002-03-13
  • 打赏
  • 举报
回复
TO:cyberworm(虫子)

“省略实例是引用的方法都被加上了关键字this”,那么,在A的构造器中调用的应该是a.dosomething()才对呀,为什么是b.dosomething()呢?
RedGuest 2002-03-13
  • 打赏
  • 举报
回复
<Thinking in java>里好象有吧
依稀记得顺序是:
1.先类成员变量申明(都给缺省值),
2.基类-本身
3.然后是申明时候的初始化,在然后是构造方法
象上面的例子,我觉得顺序应该是
avar = 0; bvar = 0; a.constructor(); bvar = 2222; b.constructor()
cyberworm 2002-03-13
  • 打赏
  • 举报
回复
我同意楼上脸红的那位,这其实是一道动态绑定的问题。不要忘了,省略实例是引用的方法都被加上了关键字this。所以在a的构造器中调用的实际是b的dosomething().
micheas 2002-03-13
  • 打赏
  • 举报
回复
关注,UP一下!
我也想知道对于这个问题的比较权威的原话..........
sha256cn 2002-03-12
  • 打赏
  • 举报
回复
我问的就是初始化先后顺序啊,是先int bvar=2222还是先执行B的CONSTRUCTOR呢?有哪本书有这方面的详细介绍?能引用些JAVA书籍的原话吗?即在哪本书上的哪一章有这方面的介绍,我好研究一下,
waterdragonfly 2002-03-12
  • 打赏
  • 举报
回复
因为b继承自a,并且覆盖了dosomething()方法,在产生新的b实例时,父类先进行构造
public a(){
System.out.println("AAA");
dosomething();
}
这里的dosomething(); 实际上是调用了b里的dosomething(); 方法,由于初始化先后顺序的问题,此时Bvar还未被初始化,所以Bvar=0
pengji 2002-03-12
  • 打赏
  • 举报
回复
这 道 题 目 问 过 N遍 了 !
sha256cn 2002-03-12
  • 打赏
  • 举报
回复
能引用些JAVA书籍的原话吗?即在哪本书上的哪一章有这方面的介绍,我好研究一下,谢谢!
deeprising 2002-03-12
  • 打赏
  • 举报
回复
当a()结束后class b中的变量才初始化(bvar=2222)

23,407

社区成员

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

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