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.

时间: 2024-10-12 01:18:27

Summary: Final Keyword的相关文章

Final Keyword In Java

The final keyword in java is used to restrict the user. The java final keyword can be used in many context. like: 1. variable 2. method 3. class 1) Java final variable If you make any variable as final, you cannot change the value of final variable(I

【笔试题】Java final keyword

Java 知识测试 Java final keyword Question 1 What is the use of final keyword in Java? A. When a class is made final, a sublcass of it can not be created. B. When a method is final, it can not be overridden. C. When a variable is final, it can be assigned

java final keyword

基于上下文.java的keywordfinal有细微的差别.但一般是指“这不能改变.”原因不希望从两个改变:一个是效率,还有一个设计.原因有两个相差很远,所以关键分final吴可使用. 接下来介绍一下使用到fianl的三中情况:数据,方法,类. final数据    很多编程语言都有某种方法.来向编译器告知一块数据是恒定不变的. 有时数据的恒定不变是非常实用的,比如: 1.一个编译时恒定不变的常量 2,一个在执行时初始化,而你不希望它被改变. 对于编译期常量的这样的情况,编译器能够将该常量值代入

Java:The final Keyword

1 import java.util.*; 2 3 class Value { 4 int i; 5 6 public Value(int i) { 7 this.i = i; 8 } 9 } 10 11 public class FinalData { 12 13 private static Random rand = new Random(47); 14 private String id; 15 16 public FinalData(String id) { 17 this.id =

Java Final and Immutable

1. Final keyword Once a variable X is defined final, you can't change the reference of X to another object, after the initialization. But you can change the content of X if it's mutable. 2. Immutable object Once an immutable object has been created,

Use final liberally

Use the final keyword liberally to communicate your intent. The final keyword has more than one meaning: a final class cannot be extended a final method cannot be overridden final fields, parameters, and local variables cannot change their value once

Writing Final Classes and Methods

You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Objectclass does this—a number of its methods are final. You might wish to m

final field's visibiity to other threads

http://www.javamex.com/tutorials/synchronization_final.shtml Thread-safety with the Java final keyword As of Java 5, one particular use of the final keyword is a very important and often overlooked weapon in your concurrency armoury. Essentially, fin

csharp: Linq keyword example

/// <summary> /// http://www.dotnetperls.com/linq /// </summary> public partial class LinqForm : Form { const int _max = 1000000; /// <summary> /// Linq keyword /// </summary> public enum CheckLinq { Distinct, Union, Intersect, Exc