Final Keyword In Java – Final variable, Method and Class 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 variable 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 ) We got a compilation error...
Comments
Post a Comment