zoukankan      html  css  js  c++  java
  • Summary: Final Keyword

    In this tutorial we will learn the usage of final keyword. final keyword can be used along with variables, methods and classes. We will cover following topics in detail.

    1) final variable
    2) final method
    3) final class

    1. Final Variables

    final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. Lets have a look at the below code:

    class Demo{  
    
       final int MAX_VALUE=99;
       void myMethod(){  
          MAX_VALUE=101;
       }  
       public static void main(String args[]){  
          Demo obj=new  Demo();  
          obj.myMethod();  
       }  
    }

    Output:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The final field Demo.MAX_VALUE cannot be assigned
    
    	at beginnersbook.com.Demo.myMethod(Details.java:6)
    	at beginnersbook.com.Demo.main(Details.java:10)

    2. Final Methods

    A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it.

    final modifier indicates that the method may not be overridden in a derived class.

    3. Final Class

    We cannot extend a final class. 

    When applied to a class, the final modifier indicates that the class can not be used as a base class to derive any class from it. 

  • 相关阅读:
    day08作业
    day07作业
    day06作业
    day05作业
    OOAD与UML
    大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践
    大数据(2):基于sogou.500w.utf8数据hive的实践
    大数据(1):基于sogou.500w.utf8数据的MapReduce程序设计
    九大排序算法的Java实现
    数字在排序数组中出现的次数
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5141896.html
Copyright © 2011-2022 走看看