Final members of a class in Java

When a class members are final either attributes or methods, they can't be reinitialized/overriden.

Let's check out through few examples.

Example 1: (inside main method)


class A{
  
  public static void main (String[] args){
    //final members
    final int number = 10; //instantiate+initialize
    
    //access the private number variable
    System.out.println("Number is: "+number);
    
  }
}

/**
 * Output: 
 * Number is: 10
 */

But what if you try to reinitialize the number variable?


class A{
  
  public static void main (String[] args){
    //final members
    final int number = 10; //instantiate+initialize
    
    // try to update the final variable
    number = 100;
    //access the private number variable
    System.out.println("Number is: "+number);
    
  }
}

/**
 * Output: 
 *  such as
 * can not reassign a value to final
 * variable number 
 */

Example 2: (final class members)

Yes, class members can also be final but it provides few restrictions over them. Like, you can't change the variable value once you initialized.

When, attributes are final: It is not possible to reinitialize the variable value once initialized.

Like:


final int x = 10; //instantiation + initialization
x = 100; //try to change the value, but it's not. possible


//Or, 

final int x; //just declaring the final variable

x = 10; //initialize the first time
x = 100; 
//now it's produce compile time error as it's not possible reinitialize the final variable onece initialized.

Look over the following simple example now:


class Main{
  //final member 
  static final int number = 10;
  
  public static void main (String[] args) {
    //access the final static variable
    System.out.println("Number is " +number);
  }
}

/**
 * Output:.
 * Number is 10
 */

Let's try to change the number variable after initializing. (inside the main method)


class Main{
  //final member 
  static final int number = 10;
  
  public static void main (String[] args) {
    //access the final static variable
    //try to reinitialize the variable
    
    number = 100;
    System.out.println("Number is " +number);
  }
}

/**
 * Output:.
 *  such as
 * can not re assign a value to a finsl variable
 * number
 */

Even, it is not possible to access the final variable without initializing. You must initialize the final variable before accessing it.

Like:


class Main{

  public static void main (String[] args) {
    //access the final static variable
    //try to reinitialize the variable
    
    final int number= 100;
    System.out.println("Number is " +number);
  }
}

/**
 * Output:.
 *  such as
 * variable number might not have been
*  initialized
 * 
 */

Example 3: (methods are final)

When methods are final in a class, you can't override it. Means, modification of the method is not possible.

Like:


class A {
    public static void hello(){
        System.out.println("Hello Java!");
    }
}
class HelloWorld extends A{
    
    public static void hello(){
        System.out.println("Hello, Jenkov!");
    }
    public static void main(String[] args) {
        //invoke the method hello()
        hello();
    }
}

If you run the above example, what you aspect as output?

Output is:
Hello, Jenkov!

instead of Hello, Java! Because we have overriden the method and it's body in the subclass.

But what if the method is final?
Then, you get compile time error like this:
Overriden method is final.


class A {
    public static final void hello(){
        System.out.println("Hello Java!");
    }
}
class HelloWorld extends A{
    
    public static final void hello(){
        System.out.println("Hello, Jenkov!");
    }
    public static final void main(String[] args) {
        //invoke the method hello()
        hello();
    }
}

Run the above example now, and you see a simple error message in the console.

/** * * Overriden method hello() is final * * /

Note: Every method in java must have method body or return type. Unless it is abstract method. Really?

Otherwise, you will get error like this: Method body is missing.