Menu
Topics Index
...
`

Final keyword915


If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword915
What will be the output of the following program?
public class Final
{
    final int assign;

    public static void main(String[] args)
    {
        final int result = 20;

        Final f = new Final();
        f.assign = process(result);
        System.out.println(f.assign);
    }
    
    final static int process(int a)
    {
        return a + 5;
    }
}

A. 20
B. 25
C. Compilation Error
D. Runtime Error
Topic: final Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans ic C..here we have the final keyword..final keyword is used to make the variable value to fixed through out the program if we try to change the value of the final variable value than compiler will raise an error..
Execution:
here we have the assign as instance variable which is final so we can not change the value of this variable..in the main() here we have another local variable which is final and assigned wiith the 20 so we can not change the value of it through out the porgram next we create the instance for the Final class so here memory is allocated for the assign variable and default value is assigned is 0 so we can not change the value of it...next we are calling the process by passing the result variable in that method we are returning the value from that method so here it will return 25 and we are assigning to it the assign variable in the instance f so here compiler will raise an error Bcz it is alrdy defined with the 0

Posted by Uday Kumar    2015-02-11 14:20:31


c

Posted by Shubham Bansal    2015-02-11 16:24:56


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-12 04:58:54


© meritcampus 2019

All Rights Reserved.

Open In App