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.
What is the output ? public class Airplane
Moderator: admin
-
- Posts: 12
- Joined: Mon Sep 09, 2019 2:08 pm
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: What is the output ? public class Airplane
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.
This is explained in OCP Java 11 Fundamentals by Hanumant Deshmukh.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Mon Sep 09, 2019 2:08 pm
- Contact:
Who is online
Users browsing this forum: No registered users and 43 guests