Java Final修饰符

Final modifier is used to declare a field as final. It can be used with variable, method or a class.

Final修饰符用于将字段声明为final。 它可以与变量,方法或类一起使用。

If we declare a variable as final then it prevents its content from being modified. The variable acts like a constant. Final field must be initialized when it is declared.

如果我们将变量声明为final,那么它将阻止内容 被修改 。 变量的作用类似于常量 。 声明最终字段时, 必须对其进行初始化

If we declare a method as final then it prevents it from being overridden.

如果我们将方法声明为final,那么它将防止其被覆盖

If we declare a class as final the it prevents from being inherited. We can not inherit final class in Java.

如果我们声明一个类为final从它可以防止继承 。 我们不能继承 Java中的最终类

示例:最终变量 (Example: Final variable)

In this example, we declared a final variable and later on trying to modify its value. But final variable can not be reassigned so we get compile time error.

在此示例中,我们声明了一个最终变量,随后又尝试修改其值。 但是最终变量无法重新分配,因此会出现编译时错误。

public class Test {
	
	final int a = 10;
	public static void main(String[] args) {
		Test test = new Test();
		test.a = 15; // compile error
		System.out.println("a = "+test.a);
	}
}

error: The final field Test.a cannot be assigned

错误:无法分配最终字段Test.a

最终方法 (Final Method)

A method which is declared using final keyword known as final method. It is useful when we want to prevent a method from overridden.

使用final关键字声明的方法称为final方法。 当我们想防止方法被覆盖时,它很有用。

示例:最终方法 (Example: Final Method)

In this example, we are creating a final method learn() and trying to override it but due to final keyword compiler reports an error.

在此示例中,我们将创建一个最终方法learning()并尝试覆盖它,但是由于final关键字编译器报告错误。

class StudyTonight
{
  final void learn()
  {
    System.out.println("learning something new");
  }
}

// concept of Inheritance
class Student extends StudyTonight
{
  void learn()
  {
    System.out.println("learning something interesting");
  }

  public static void main(String args[]) {
    Student object= new Student();
    object.learn();
  }
}

Cannot override the final method from StudyTonight

无法覆盖StudyTonight的最终方法

This will give a compile time error because the method is declared as final and thus, it cannot be overridden. Don't get confused by the extends keyword, we will learn about this in the Inheritance tutorial which is next.

这将产生编译时错误,因为该方法被声明为final方法,因此不能被覆盖。 不要对extends关键字感到困惑,我们将在接下来的继承教程中学习到这一点。

Let's take an another example, where we will have a final variable and method as well.

让我们再举一个例子,这里我们还将有一个最终变量和方法。

class Cloth
{
  final int MAX_PRICE = 999;    //final variable
  final int MIN_PRICE = 699;
  final void display()      //final method
  {
    System.out.println("Maxprice is" + MAX_PRICE );
    System.out.println("Minprice is" + MIN_PRICE);
  }
}

In the class above, the MAX_PRICE and MIN_PRICE variables are final hence there values cannot be changed once declared. Similarly the method display() is final which means even if some other class inherits the Cloth class, the definition of this method cannot be changed.

在上面的类中,MAX_PRICE和MIN_PRICE变量是最终变量,因此一旦声明就无法更改值。 同样,方法display()是最终的,这意味着即使某个其他类继承了Cloth类,该方法的定义也无法更改。

最终班 (Final Class)

A class can also be declared as final. A class declared as final cannot be inherited. The String class in java.lang package is an example of a final class.

一个类也可以声明为final。 声明为final的类不能继承。 java.lang包中的String类是最终类的示例。

We can create our own final class so that no other class can inherit it.

我们可以创建自己的最终类,以便其他任何类都不能继承它。

示例:最终班 (Example: Final Class)

In this example, we created a final class ABC and trying to extend it from Demo class. but due to restrictions compiler reports an error. See the below example.

在此示例中,我们创建了最终类ABC,并尝试将其从Demo类扩展。 但是由于限制,编译器会报告错误。 请参见以下示例。

final class ABC{
	
	int a = 10;
	void show() {
		System.out.println("a = "+a);
	}
	
}

public class Demo extends ABC{
	
	public static void main(String[] args) {
		
		Demo demo = new Demo();
		
	}
}

The type Demo cannot subclass the final class ABC

演示类型不能将最终类ABC子类化

Java空白最终变量 ( Java Blank Final Variable )

Final variable that is not initialized at the time of declaration is called blank final variable. Java allows to declare a final variable without initialization but it should be initialized by the constructor only. It means we can set value for blank final variable in a constructor only.

在声明时尚未初始化最终变量称为空白最终变量 。 Java允许声明最终变量而无需初始化,但是只能由构造函数 初始化 。 这意味着我们只能在构造函数中为空白的最终变量设置值。

例: (Example:)

In this example, we created a blank final variable and initialized it in a constructor which is acceptable.

在此示例中,我们创建了一个空白的最终变量,并在可接受的构造函数中对其进行了初始化。

public class Demo{
	// blank final variable
	final int a;
	
	Demo(){
		// initialized blank final
		a = 10;
	}
	
public static void main(String[] args) {
		
	Demo demo = new Demo();
	System.out.println("a = "+demo.a);
		
	}
}

a=10

a = 10

静态空白最终变量 (Static Blank Final Variable)

A blank final variable declared using static keyword is called static blank final variable. It can be initialized in static block only.

使用static关键字声明的空白最终变量称为静态空白最终变量。 它只能在静态块中初始化。

Static blank final variables are used to create static constant for the class.

静态空白最终变量用于为该类创建静态常量。

(Example)

In this example, we are creating static blank final variable which is initialized within a static block. And see, we used class name to access that variable because for accessing static variable we don’t need to create object of that class.

在此示例中,我们正在创建静态空白最终变量,该变量在静态块内初始化。 并且看到,我们使用类名来访问该变量,因为访问静态变量时,我们不需要创建该类的对象。

public class Demo{
	// static blank final variable
	static final int a;
	
	static{
		// initialized static blank final
		a = 10;
	}
	
public static void main(String[] args) {
		
	System.out.println("a = "+Demo.a);
		
	}
}

a=10

a = 10

翻译自: https://www.studytonight.com/java/final-in-java.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值