This program is used to implement final variable. Final variable is constant variable which is unchanged.
import java.io.*;
import java.util.*;
class increment
{
private int i;
private final int J;
increment()
{
J=2; // final value
}
int add(int c)
{
int x=c+J;
return x;
}
}
class total
{
public static void main(String args[])
{
increment t=new increment();
int a=2;
System.out.println("Before invoking final value:"+a);
int b1=t.add(a);
System.out.println("After invoking final value:"+b1);
int b=t.add(b1);
System.out.println("After invoking final value:"+b);
}
}
Output
Before invoking final value:2
After invoking final value:4
After invoking final value:6
import java.io.*;
import java.util.*;
class increment
{
private int i;
private final int J;
increment()
{
J=2; // final value
}
int add(int c)
{
int x=c+J;
return x;
}
}
class total
{
public static void main(String args[])
{
increment t=new increment();
int a=2;
System.out.println("Before invoking final value:"+a);
int b1=t.add(a);
System.out.println("After invoking final value:"+b1);
int b=t.add(b1);
System.out.println("After invoking final value:"+b);
}
}
Output
Before invoking final value:2
After invoking final value:4
After invoking final value:6
No comments:
Post a Comment