Final Keyword

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 66:-  Final Keyword

In Java, the final keyword is used to declare entities that cannot be changed after initialization. It can be applied to variables, methods, and classes to enforce immutability, prevent method overriding, and prevent class inheritance, respectively.

  1. Final Variables: When applied to a variable, the final keyword makes it a constant, meaning its value cannot be changed once it is initialized. Final variables must be assigned a value either during declaration or within the constructor of the class.

    javaCopy code

    public class MyClass {    final int constantValue = 10; // Final variable    public void changeValue() {        // Error: Cannot change the value of a final variable        // constantValue = 20;    } }

  2. Final Methods: When applied to a method, the final keyword prevents the method from being overridden by subclasses. This is often used to ensure that certain methods cannot be modified in subclasses, maintaining the intended behavior of the base class method.

    javaCopy code

    public class BaseClass {    public final void doSomething() {        // Method implementation    } } public class SubClass extends BaseClass {    // Error: Cannot override a final method    // public void doSomething() {    //    // Modified implementation (Not allowed)    // } }

  3. Final Classes: When applied to a class, the final keyword prevents the class from being subclassed (inherited). This is useful when you want to create a class that should not have any subclasses.

    javaCopy code

    public final class FinalClass {    // Class implementation } // Error: Cannot subclass a final class // public class SubClass extends FinalClass { //    // Subclass implementation (Not allowed) // }

Benefits of Using final:

  • Safety: Marking variables as final ensures that their values remain constant, preventing unexpected changes and reducing bugs.
  • Performance: The final keyword allows the compiler to perform optimizations, as it knows the value of the final variable won't change.
  • Code Clarity: Using final on methods and classes explicitly communicates that they should not be modified or overridden, making the code easier to understand and maintain.

Note:

  • If a reference variable is declared as final, it means the reference cannot be changed, but the object it refers to can still be modified (the state of the object can be changed).
  • For arrays, if the array reference is declared as final, it means the reference cannot be changed, but the contents of the array can still be modified.

javaCopy code

final int[] arr = {1, 2, 3}; // Error: Cannot change the reference // arr = new int[]{4, 5, 6}; // But, the contents of the array can be modified arr[0] = 10; // Valid

10. Classes and Objects

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?