Difficulty level : Easy
Prerequisite : final keyword in java

Predict the output of following Java Programs:
- What will be output of following program?
classTest{finalintMAXIMUM;finaldoublePI;publicTest(intmax){MAXIMUM = max;}publicTest(doublepi){PI = pi;}publicstaticvoidmain(String[] args){Test t1 =newTest(1500);Test t2 =newTest(3.145);System.out.println("MAXIMUM : "+ t1.MAXIMUM +" PI : "+ t2.PI);}}a) Compilation error
b) Runtime error
c) 1500 3.145
d) 3.145 1500Ans. a) Compilation error
Explanation : As we know that we can initialize a blank final variable inside constructor also, but if there are more than one constructors then it is mandatory to initialize all final variables in all of them. This is because we can create an object of class by calling any one of the constructors, but if that constructor is not initializing any one of declared final variable than there is problem.
- What will be output of following program?
classTest{finalintMAXIMUM = m1();privateintm1(){System.out.println(MAXIMUM);return1500;}publicstaticvoidmain(String[] args){Test t =newTest();System.out.println(t.MAXIMUM);}}a) Compilation error
b) Runtime error
c) 0
1500
d) 1500
1500Ans. c)
Explanation : A final variable can only be initialized once, either via an initializer or an assignment statement. Also you might think that in above program, MAXIMUM is initialized two times.This is wrong. The output is based on the fact that JVM first initialize any(final or normal) variable with it’s default-type value and then look through assignment statement(if any).
- What will be output of following program?
// filename : Test1.javafinalinterfaceTest{intMAXIMUM =1500;voidm1();}classTest1implementsTest {@Overridepublicvoidm1(){System.out.println("From Test1 m1 method");}publicstaticvoidmain(String[] args){newTest1().m1();}}a) Compilation error
b) Runtime error
c) From Test1 m1 methodAns. a) Compilation error
Explanation : Interfaces can never be declared final as they are meant to be implemented in derived classes. Please see interface and inheritance
- What will be output of following program?
classTest {publicstaticvoidmain(String[] args){intarr[] = {1,2,3};// final with for-each statementfor(finalinti : arr)System.out.print(i +" ");}}a) Compilation error
b) Runtime error
c) 1 2 3Ans. c) 1 2 3
Explanation : Since the variable i goes out of scope with each iteration of the loop, it is actually getting re-declared after each iteration, allowing the same token (i.e. i) to be used to represent multiple variables.
- What will be output of following program?
classTest {publicstaticvoidmain(String[] args){finalStringBuilder sb =newStringBuilder("Geeks");sb.append("ForGeeks");System.out.println(sb);}}a) Compilation error
b) Runtime error
c) Geeks
d) GeeksForGeeksAns. d) GeeksForGeeks
Explanation : In case of a reference final variable(here sb), internal state of the object pointed by that reference variable can be changed. Note that this is not re-assigning. This property of final is called non-transitivity.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



