I was reading the OCPJP (SCJP) book, when I came across this question in chapter 2.
class Uber{
static int y= 2;
Uber(int i){
this();
y = y*2;
}
Uber(){
y++
}
}
class Minor extends Uber{
Minor(){
super(y);
y=y+3;
}
public static void main(String[] args){
new Minor();
System.out.println(y);
}
}
The book says the answer should be 9, but I’m confused as to whether the compiler will recognize the y in super(y);. If it does how can that be?
Thanx in advance.
No, the code you’ve given won’t compile. It would compile if
MinorextendedUberthough – are you sure it doesn’t in the book?(Even after getting it to compile, the code prints 7 for me, not 9. I can’t see how it would end up printing 9… it starts at 2, then is doubled to 4, then 3 is added to get 7…)