Final keyword in Java
Final Keyword :
In java programming final can be :
- Variable
- Method
- Class
1. Java final variable
In Java, to make constant we use final keyword.
If you make any variable as final, you cannot change the value of that variable.
Naming convention for final variable is, write in upper case..
public class Test { final int X =20; void calculate() { X = 30; } }OUTPUT :
Compile Time ERROR
2. Java final method
If you make any method as final, you cannot override it.
class Parent { final void calculate() { //definition } } class Child extends Parent { void calculate() { //definition } }OUTPUT :
Compile Time ERROR
3. Java final class
If you make any class as final, you cannot extend it.
final class Parent { void calculate() { //definition } } class Child extends Parent { void show() { //definition } }OUTPUT :
Compile Time ERROR
Does final method inherited ?
Answer :
Yes,final method is inherited but you cannot override it.
What is blank or uninitialized final variable ?
Answer :
A final variable that is not initialized at the time of declaration is known as blank final variable.
Can we initialize blank final variable ?
Answer :
Yes, but only in constructor.
class Test { final int X; Test() { X = 10; } }
What is static blank final variable ?
Answer :
A static final variable that is not initialized at the time of declaration.
It can be initialized only in static block.
class Test { static final int X; static { X = 10; } }
Next topic is abstract-class
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in