Page 1 of 1

What is the output ? public class Airplane

Posted: Sat Mar 21, 2020 2:08 pm
by zuzu007
Hi! Can you please help me with this question? I answered option D. I thought that the var end can't be changed in the constructor because de var end is final. If it was a method it couldn't be changed but because is a constructor you can? Please let me know if my logic is correct. Thanks!

What is the output of the following application?
public class Airplane {
static int start = 2;
final int end;
public Airplane(int x) {
x = 4;
end = x;
}
public void fly(int distance) {
System.out.print(end-start+" ");
System.out.print(distance);
} public static void main(String... start) {
new Airplane(10).fly(5);
}
}
A. 2 5
B. 8 5
C. 6 5
D. The code does not compile.

Re: What is the output ? public class Airplane

Posted: Sat Mar 21, 2020 9:36 pm
by admin
yes, a final variable is allowed to be initialized once before use in the constructor. Try it out.
This is explained in OCP Java 11 Fundamentals by Hanumant Deshmukh.

Re: What is the output ? public class Airplane

Posted: Sun Mar 22, 2020 12:24 am
by zuzu007
Thank you!