The final is a modifier in Java, which can be applied to a variable, a method or a class.When a final modifier is used with a class then the class cannot be extended further. This is one way to protect your class from being subclassed and often sensitive classes are made final due to security reason.
For the Static and instance variables no need to perform initialization, JVM Will Provide default initialization.For the local variables compulsory we should perform initialization before using.
Along with lambda expression, streams and a couple of other changes, Java 8 has introduced a new concept called effectively final variable, which allows a non-final variable to be accessed inside an inner class or lambda expression.
Earlier, you cannot access a non-final local variable inside an inner or anonymous class but from Java 8 onwards you can provide it's effectively final i.e. it's value has not changed after assignment.
When you make a class final than all its method effectively become final because a final class cannot be extended and without inheritance, you cannot override them. Some programmers argue that how about overriding final methods in the inner class, well that's not true because even an inner class cannot extend a final class.
Final Instance Variables
For the final instance variables JVM won’t provide any default values, compulsory we should
perform initialization before completion of constructor.
The following are the places to perform this
1) At the time of declaration:.
final int i = 0;
2) Inside instance initialization class
Visibility public protected <default> private With in the same classWith in the same package but from out side class.
Outside package but from child class
Outside package but from non-child
class Test
final int i;
{
i = 0;
}
3) Inside constructor
final int i;
test()
{
i = 0;
}
If u r performing initialization any where else we will get compile time error.
Ex:
class Test
{
final int i;
}
Output:- Generates Compile time error, variable “i” might not have been initialized.
Class Test
{
final int i;
public void m1()
{
i=20;
}
}
Output:- Generates Compile time error, can’t assign a value to final variable.
Class Test
{
final int i;
{
i=20;
}
}
Output- Won’t generate compile time error.
Final static Variables
For the final static variables compulsory we should perform initialization.
Ex:
class Test
{
final static int i;
}
Output:-generates Compile time error. variable i might not have been assigned.
We can perform initialization for the final static variables in the following places.
1) At the time of declaration
final static int i = 0;
2) Inside static blocks
static
{
i = 0;
}
3) If u r performing initialization anywhere else we will get compile time error
class Test
{
final static int i;
public static void main(String arg[])
{
i = 20;
}
}
Output:- will produce compile time error.
Final local Variables
Before using a local variable(whether it is final or non-final) we should perform initialization. If we are not using local variable then no need of perform initialization even though it is final.
Ex:
class Test
{
public static void main(String arg[])
{
int i;
System.out.println("hello");
}
}
This is valid Because we didn’t use “i” here.
class Test
{
public static void main(String arg[])
{
final int i;
System.out.println("hello");
}
}
This is also valid because we didn’t use “i” here.
class Test
{
public static void main(String arg[])
{
final int i = 0;
System.out.println("hello");
}
}
This is valid..
class Test
{
public static void main(String arg[])
{
final int i;
System.out.println(i);
}
}
This is not valid. Because Before going to use we didn’t initialize “i”.
The variables which are declared as formal are acts as local variables of the method.
if a formal parameter declared as the final then with in the method we are not allowed to change it’s values.
Ex:
class Test
{
public static void main(String arg[])
{
m1(10,20);
}
public static void m1(final int i,final int j)
{
//i=100;
//j=100;
System.out.println(i+"----"+j);
}
}
If you use final you can’t change i, j values.
Final parameter ‘i’ may not be assigned.
For the local variables the only applicable modifier is final.
Ex:
class Test
{
public static void main(String arg[])
{
private int i = 20;
System.out.println(i);
}
}
Output:
Provides compile time error because we declare int as private.
Also Read post: Java User Defined Exception with example
When the final keyword is used with a method that it cannot be overridden in Java, which means you cannot override the logic of the method in the subclass. This is also done to protect the original logic of method.
When the final keyword is used with a variable then its value cannot be changed once assigned. Though this is a little bit tricky to understand especially when you make a reference variable final pointing to array or collection.
For the Static and instance variables no need to perform initialization, JVM Will Provide default initialization.For the local variables compulsory we should perform initialization before using.
Along with lambda expression, streams and a couple of other changes, Java 8 has introduced a new concept called effectively final variable, which allows a non-final variable to be accessed inside an inner class or lambda expression.
Earlier, you cannot access a non-final local variable inside an inner or anonymous class but from Java 8 onwards you can provide it's effectively final i.e. it's value has not changed after assignment.
When you make a class final than all its method effectively become final because a final class cannot be extended and without inheritance, you cannot override them. Some programmers argue that how about overriding final methods in the inner class, well that's not true because even an inner class cannot extend a final class.
Final Instance Variables
For the final instance variables JVM won’t provide any default values, compulsory we should
perform initialization before completion of constructor.
The following are the places to perform this
1) At the time of declaration:.
final int i = 0;
2) Inside instance initialization class
Visibility public protected <default> private With in the same classWith in the same package but from out side class.
Outside package but from child class
Outside package but from non-child
class Test
final int i;
{
i = 0;
}
3) Inside constructor
final int i;
test()
{
i = 0;
}
If u r performing initialization any where else we will get compile time error.
Ex:
class Test
{
final int i;
}
Output:- Generates Compile time error, variable “i” might not have been initialized.
Class Test
{
final int i;
public void m1()
{
i=20;
}
}
Output:- Generates Compile time error, can’t assign a value to final variable.
Class Test
{
final int i;
{
i=20;
}
}
Output- Won’t generate compile time error.
Final static Variables
For the final static variables compulsory we should perform initialization.
Ex:
class Test
{
final static int i;
}
Output:-generates Compile time error. variable i might not have been assigned.
We can perform initialization for the final static variables in the following places.
1) At the time of declaration
final static int i = 0;
2) Inside static blocks
static
{
i = 0;
}
3) If u r performing initialization anywhere else we will get compile time error
class Test
{
final static int i;
public static void main(String arg[])
{
i = 20;
}
}
Output:- will produce compile time error.
Final local Variables
Before using a local variable(whether it is final or non-final) we should perform initialization. If we are not using local variable then no need of perform initialization even though it is final.
Ex:
class Test
{
public static void main(String arg[])
{
int i;
System.out.println("hello");
}
}
This is valid Because we didn’t use “i” here.
class Test
{
public static void main(String arg[])
{
final int i;
System.out.println("hello");
}
}
This is also valid because we didn’t use “i” here.
class Test
{
public static void main(String arg[])
{
final int i = 0;
System.out.println("hello");
}
}
This is valid..
class Test
{
public static void main(String arg[])
{
final int i;
System.out.println(i);
}
}
This is not valid. Because Before going to use we didn’t initialize “i”.
The variables which are declared as formal are acts as local variables of the method.
if a formal parameter declared as the final then with in the method we are not allowed to change it’s values.
Ex:
class Test
{
public static void main(String arg[])
{
m1(10,20);
}
public static void m1(final int i,final int j)
{
//i=100;
//j=100;
System.out.println(i+"----"+j);
}
}
If you use final you can’t change i, j values.
Final parameter ‘i’ may not be assigned.
For the local variables the only applicable modifier is final.
Ex:
class Test
{
public static void main(String arg[])
{
private int i = 20;
System.out.println(i);
}
}
Output:
Provides compile time error because we declare int as private.
Also Read post: Java User Defined Exception with example
No comments:
Post a Comment