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(It wil be constant).

Example of final variable

There is a final variable limit, we are going to change the value of this variable, but it can‘t be change because final variables once assgined a value can nerver be changed.

public class FinalTest {
	private final int i = 90; //final variable

	public static void main(String[] args) {

	}

	public void run() {
		i = 12;
	}
}

Output:Compile Time Error

2) Java final method

If you make any method as final, you cannot override it.

Example of final method

public class FinalTest {
	public final void run(){
		System.out.println("running safely with 90kmph");
	}

}

class Honda extends FinalTest {
	@Override
	public void run() {
		System.out.println("running safely with 100kmph");
	}
	public static void main(String args[]) {
		Honda hd = new Honda();
		hd.run();
	}
}

Output:Compile Time Error

3) Java final class

If you make any class as final, you cannt extend it.

Example of final class

public final class FinalTest {
	public final void run(){
		System.out.println("running safely with 90kmph");
	}

}

class Honda extends FinalTest {
	public static void main(String args[]) {
		Honda hd = new Honda();
		hd.run();
	}
}

Output:Compile Time Error

Q) Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it.

Q) What is blank or uninitialized final variable?

A final avariable that is not initialized at the time of declaration is known as blank final avariable.

If you want to create a variable that is initialized at the time of createing object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee. It can be initialize only in constructor.

Example of blank final method

public class FinalTest {
	private final String panCard;
	public FinalTest(){
		panCard = "hagendasi";
	}

}

static blank final variable

A static final avariable that is not initialized at the time of declaration is known as static blank final avariable. It can be initialized only in static block.

Example of blank final method

public class FinalTest {
	private static final String panCard;
	static {
		panCard = "staticBlock";
	}
	public static void main(String[] args) {
		System.out.println(FinalTest.panCard);
	}
}

Q) What is final parameter?

If you declare variable as final, you cannot change the value of it.

public class FinalTest {
	public static void main(String[] args) {
		FinalTest ft = new FinalTest();
		ft.finalParameter(5);
	}

	public void finalParameter (final int i) {
		i = i+1;
	}
}

Output:Compile Time Error

Q) Can we declare a constructor final?

No, because constructor is never inherited.

原文地址:https://www.cnblogs.com/chenqr/p/10369053.html

时间: 2024-10-29 08:09:40

Final Keyword In Java的相关文章

【笔试题】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,一个在执行时初始化,而你不希望它被改变. 对于编译期常量的这样的情况,编译器能够将该常量值代入

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 variable2) final method3) final class 1. Final Variables final variable

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 =

内部类访问外部类的变量必须是final吗,java静态方法中不能引用非静态变量,静态方法中不能创建内部类的实例

内部类访问外部类的变量必须是final吗? 如下:class A{int i = 3;public void shout(){ class B{public void shout1(){System.out.println(i);} }B b=new B();b.shout1();} public static void main(String [] args){A a=new A();a.shout();} }可正常输出3,证明可以访问类的变量i,但改为下面的方式:class A{public

Java中类的继承,属性和方法的四种修饰符的作用范围,final关键字,java的三大特点中的2个:封装和多态,以及多态的一个设计模式,模板方法模式(template method)

(一)Java中的继承: 关于继承,在Java中类的继承只能是单继承,不像C+++那样灵活,可以多继承,多继承的后果就是各种关系乱套,就相当于一个孩子有2个母亲一样,社会关系的复杂,不利于程序后期的开发和维护,所有Java中的类是只能单继承,通过接口来实现多继承:对于接口的话,是可以多继承的,例如:A接口可以同时继承接口B和接口C,但是有一点要注意,就是实现接口A的类必须要实现接口A.B.C中所有的抽象方法:接口之所以可以多继承,就是因为接口结构比较简单. 继承是通过extends关键字来实现,

【Java学习笔记之二十】final关键字在Java继承中的用法小结

谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法. 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括成员变量和局部变量).下面就从这三个方面来了解一下final关键字的基本用法. 1.修饰类   当用final修饰一个类时,表明这个类不能被继承.也就是说,如果一个类你永远不会让他被继承,就可以用final

Ubuntu 16.04 LTS Final Beta about JAVA

我们知道Ubuntu里可能会事先安装了openjdk.但是我习惯于Oracle jdk. Markdown 语法书写格式: #2016.03.29 ## < 卸载 openjdk > successfully: ### Terminal command:sudo apt-get purge openjdk/openjdk*;sudo apt-get clean/autoclean; 我们此时应该是把 openjdk 从系统中删除了. ##< 下载安装 Oracle jdk > su

深入理解final关键字以及一些建议

引子:一说到final关键字,相信大家都会立刻想起一些基本的作用,那么我们先稍微用寥寥数行来回顾一下. 一.final关键字的含义 final是Java中的一个保留关键字,它可以标记在成员变量.方法.类以及本地变量上.一旦我们将某个对象声明为了final的,那么我们将不能再改变这个对象的引用了.如果我们尝试将被修饰为final的对象重新赋值,编译器就会报错. 二.用法 1.修饰变量 final修饰在成员变量或者局部变量上,那么我们可以称这个变量是final变量,这可能使我们用到最多的地方,举个栗