Home ProgrammingJava Non Access Java Modifier – FINAL
Non Access Java Modifiers

Non Access Java Modifier – FINAL

by Log Raj
850 views

Non Access Java modifier

We have already covered some introduction about Java modifiers as well as access control java modifiers. So this article will be totally focused on Non Access Java Modifier more specifically final. All java modifier other than Access control java modifiers are Non access Java Modifiers. There are about 8 non access java modifiers. These are-

    • final,
    • abstract,
    • static,
    • strictfp,
    • native,
    • synchronized,
    • transient,
    • volatile

I’ll try to explain all of these in brief. Now lets begin from first one

final:

Final as the name suggest is the component that never gone change. I’ll try to present important facts about final in following points so that you can easily understand and remember.

    1. Final can be applied with class, method and variables.
    2. A final class can not be extended by other classes. We use final keyword with classes which are secure and we don’t want anyone to make subclass of these classes. For example in java library String, StringBuilder or other wrapper classes like Integer, Float etc are final. There for We can not extend these classes. For example following code is completely invalid.
      package log.pyarb.main;
      public class MainClass extends Integer  {
      //Compile time error -- The type MainClass can not subclass the final class Integer
      }
      
    3. You can not override final methods. We use final keyword with methods to protect the original logic of the method.
      Studio.java

      package log.pyarb.main.finalPack;
      public class Studio {
              public String songName;
      	public final void recordSong() {
      		System.out.println("Ding Dong!!");
      	}
      }

      MainClass.java

      package log.pyarb.main;
      import log.pyarb.main.finalPack.Studio;
      public class MainClass extends Studio{
      	@Override
      	public void recordSong() {
      		//Compile time error- Cannot override the final method from studio 
      	}
      }
      
    4. Final variables value can not be changed once we assign a value to final variable. It doesn’t mean we can not add new element to the final collection. But we can not assign a new collection if the reference variable is final. I want to show this by example.
      package log.pyarb.main;
      import log.pyarb.main.finalPack.Studio;
      public class MainClass{
      	public static void main(String[] args) {
      		 final Studio kutumba=new Studio();
      		 kutumba.song="sdf";
                       Studio std=new Studio();
      		 kutumba=std;// This is invalid and rise compile time error
      	}
      }

      Here reference kutumba is final and having a object. Now we can manipulate object properties but can not reassign it with new Object.

    5. Making a method final inside final class has no sense because all the methods inside final class are automatically final.
    6. We can use final keyword with local as well as instance variables.
    7. Also We can not use both final And abstract keywords with any class or method.
    8. We must have to initialize the final instance variable at the time of declaration or more specifically before the object creation(constructor call) so we can initialize final variable inside initializer block also. Because initializer block execute before the constructor.
      package log.pyarb.main;
      import log.pyarb.main.finalPack.Studio;
      public class MainClass{
      	final int count;// Initialize here
      	{
      		// Or Here You can initialize
      	}
      	MainClass(){
      		
      		count=0;// Last option, Intialize here otherwise compile time error
      	}
      

    Hope this will help you understanding the basics of final keyword.

Non Access Java Modifier – FINAL

by Log Raj time to read: 2 min
0