• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q5+Q6 Dan

 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've seen no problem with Q5 - compiler could see that |this| stands on the second place. But in Q6 compiler doesn't know that |j| can't reached before R() construction. Why compiler error???
Thank you guys for previous answers!
[ August 27, 2002: Message edited by: Dmitry Golynkin ]
[ August 27, 2002: Message edited by: Dmitry Golynkin ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this() or super() explicit constructor invocation you are not allowed to pass instance members (like j for instance) because the supertype is initialized prior to the subtype, thus j is not yet initialized when the superclass constructor is invoked.
From JLS 8.8.5.1 Explicit Constructor Invocations


An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

If you have a computer at hand you could try to compile the code and see what the compiler is saying. In this case it is saying:
cannot reference j before supertype constructor has been called
super(j);

The error message is very expressive, isn't it?
Please format your code using CODE tags.
[ August 27, 2002: Message edited by: Valentin Crettaz ]
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, my JBuilder types the same. But why did it type compiler error? What is the border between copmiler and runtime errors?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why did it type compiler error? What is the border between copmiler and runtime errors?
Please see the following recent discussion about the same topic:
http://www.coderanch.com/t/239025/java-programmer-SCJP/certification/runtime-vs-compiler-error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic