Final Variable, Method, Class

Final Variable, Method, Class

  • The final keyword in java is used to restrict the user. 
  • The java final keyword can be used in many contexts. 
  • Final can be: variable, method, class

1. Final Variable

  • When a variable is declared with the final keyword, its value can’t be modified, essentially, a constant. 
  • This also means that you must initialize a final variable. 

  • Initializing a final variable:
    • We must initialize a final variable, otherwise compiler will throw compile-time error.
    • A final variable can only be initialized once, either via an initializer or an assignment statement. 
    • There are three ways to initialize a final variable :
    • You can initialize a final variable when it is declared.This approach is the most common. 
    • Syntax:

final datatype variable_name = value;

    • Example:

final int age=10;

  • A final variable is called blank final variable,if it is not initialized while declaration. 
  • A blank final variable can be initialized inside instance-initializer block or inside constructor. 


  • Syntax:

final datatype variable_name;

  • Example:

final int age;
{
age=10;
}


  • A blank final static variable can be initialized inside static block.
  • Syntax:

final static datatype variable_name;

  • Example:

final static int age;
static{
age=10;
}


  • Full Example:

class Bike{ 
 final int speedlimit=90;
 void run(){ 
  speedlimit=400; 
 } 
 public static void main(String args[]){ 
 Bike obj=new  Bike(); 
 obj.run();  //Output:Compile Time Error
 } 
}

2. Final Method

  • When a method is declared with 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 final keyword for which we required to follow the same implementation throughout all the derived classes. 
  • Example:

class A
{
    final void method()
    {
        System.out.println("This is a final method.");
    }
}

class B extends A
{
    void method()
    {
        // COMPILE-ERROR! Can't override.
        System.out.println("Illegal!");
    }
}


  • Full Example:

class Bike{ 
  final void run()
{
System.out.println("running");


   
class Honda extends Bike{ 
   void run()
{
System.out.println("running safely with 100kmph");

   public static void main(String args[])

   Honda honda= new Honda(); 
   honda.run();  //Output:Compile Time Error
   



  • Full Example:

class Bike{ 
  final void run()
{
System.out.println("running...");


class Honda2 extends Bike

   public static void main(String args[])

new Honda2().run(); 



3. Final Class

  • When a class is declared with final keyword, it is called a final class. 
  • A final class cannot be extended(inherited). 
  • There are two uses of a final class :
  • One is definitely to prevent inheritance, as final classes cannot be extended. 
  • For example, all Wrapper Classes like Integer,Float etc. are final classes. 
  • We can not extend them.
  • Example:

final class A
{
     // methods and fields
}

class B extends A
{
    // COMPILE-ERROR! Can't subclass A
}


  • The other use of final with classes is to create an immutable class like the predefined String class.
  • You can not make a class immutable without making it final.
  • Full Example:

final class Bike{} 
 
class Honda1 extends Bike

  void run()
{
System.out.println("running safely with 100kmph");

  public static void main(String args[])

  Honda1 honda= new Honda1(); 
  honda.run(); 

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form