In java there 3 use cases of final keyword
1) Final variables
2) Final method
3) Final class
each use case has its own advantages and Disadvantages.
1). Final Variables
a). Final Variables
If variables marked as final can't be reassigned. Once a final variable is initialized its value cannot be changed.
b). Final Reference Variables
If the reference variable declared as final then we can't perform reassignment for the reference variable it doesn't mean we can't perform any change in that object. suppose we have final reference variable cat and initialize it:
final Bike bike = new Bike();
bike = new Bike(); // not allowed
but we can do like
bike.sePrice(90000);
bike.setColor("Blue");
c). Final Primitive Variables
if we assign value to primitive types it gives the compile-time error
roll=125;
final int roll= 123;
The final local variable xxx may already have been assigned.
d). Final fields
Generally these fields are used as constants, generally defined in the uppercase letters. If too long then underscore may be applied as convention
static final String NEWLINE = "\n";
static final int MAX_UPLOAD_SIZE = 1024*1024*10;
static field used so that they can be accessed anywhere without creating objects of call.
When to use a final variable :
The only difference between a normal variable and a final variable is that we can re-assign value to a normal variable but we cannot change the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of the program.
2). Final method
When a method is declared with the final keyword, it is called a final method.
A final method cannot be overridden.
The Object class does this—a number of its methods are final.
We must declare methods with the final keyword for which we required to follow the same implementation throughout all the derived classes.
example:
{ final void property() { System.out.println("This is a final method in parent."); } } class Child extends Parent { void property() { System.out.println("Can't override!"); } }
class Parent
3). Final Class
Classes marked as final can’t be extended. Any attempt to inherit from a final class will cause a compiler error. There are two uses of a final class :
1). To prevent inheritance, final classes cannot be extended. For example, all Wrapper Classes like Integer, Float, etc. are final classes. We can not extend them.
{ // methods and fields } class Child extends Parent { // COMPILE-ERROR! The type Child cannot subclass the final class Parent }
final class Parent
2). To create an immutable class like the predefined String class. You can not make a class immutable without making it final.
Immutable class means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable. ... The class must be declared as final (So that child classes can't be created)
That's it!! We have seen the use of the final keyword in java.
0 Comments
Post a Comment