静态和最终的区别? [英] Difference between Static and final?

查看:112
本文介绍了静态和最终的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是在 java static 和最终关键字之间感到困惑> 即可。



它们有何不同?

解决方案

静态关键字可以是在4个场景中使用




  • 静态变量

  • 静态方法

  • 静态代码块

  • 静态嵌套类



让我们看一下静态变量和静态方法优先。



静态变量




  • 这是一个变量属于类而不是对象(实例)。

  • 静态变量在执行开始时只初始化一次。在初始化任何实例变量之前,将首先初始化这些变量。

  • 要由类的所有实例共享的单个副本。

  • 静态变量可以通过类名直接访问,不需要任何对象。

  • 语法:类.variable



静态方法




  • 这是一个属于类的方法,而不是对象(实例)。

  • 静态方法只能访问静态数据。它不能访问非静态数据(实例变量),除非它具有/创建类的实例。

  • 静态方法只能调用其他静态方法,除非它有/创建类的实例,否则不能从中调用非静态方法。

  • 静态方法可以直接通过类名访问,不需要任何对象。

  • 语法:类.methodName()

  • 静态方法不能引用这个 super无论如何关键字。



静态类



Java也有静态嵌套类。静态嵌套类只是一个不隐式引用外部类实例的类。



静态嵌套类可以有实例方法和静态方法。 / p>

在Java中没有顶级静态类。



附注:




main方法是 static ,因为在任何实例化之前,应用程序必须是可访问的才能运行地方。




最终关键字用于几个不同的上下文来定义实体以后不能更改。




  • final 类不能被子类化。这样做是出于安全和效率的原因。因此,许多Java标准库类都是 final ,例如 java.lang.System java.lang.String中 final 类中的所有方法都隐含 final


  • 子类不能覆盖 final 方法。这用于防止子类的意外行为改变可能对类的功能或一致性至关重要的方法。


  • 一个 final 变量只能通过初始值设定项或赋值语句初始化一次。它不需要在声明点初始化:这称为空白最终变量。必须在声明它的类的每个构造函数的末尾明确赋值类的空白最终实例变量;类似地,必须在声明它的类的静态初始值设定项中明确赋值空白的最终静态变量;否则,在两种情况下都会发生编译时错误。




注意:如果变量是引用,这意味着变量无法重新绑定以引用另一个对象。但它引用的对象仍然是可变的,如果它最初是可变的。



当在方法体内定义匿名内部类时,所有变量都声明为<$可以从内部类中访问该方法范围内的c $ c> final 。分配后,最终变量的值不能更改。


I'm always confused between static and final keywords in java.

How are they different ?

解决方案

The static keyword can be used in 4 scenarios

  • static variables
  • static methods
  • static blocks of code
  • static nested class

Let's look at static variables and static methods first.

Static variable

  • It is a variable which belongs to the class and not to object (instance).
  • Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
  • A single copy to be shared by all instances of the class.
  • A static variable can be accessed directly by the class name and doesn’t need any object.
  • Syntax: Class.variable

Static method

  • It is a method which belongs to the class and not to the object (instance).
  • A static method can access only static data. It can not access non-static data (instance variables) unless it has/creates an instance of the class.
  • A static method can call only other static methods and can not call a non-static method from it unless it has/creates an instance of the class.
  • A static method can be accessed directly by the class name and doesn’t need any object.
  • Syntax: Class.methodName()
  • A static method cannot refer to this or super keywords in anyway.

Static class

Java also has "static nested classes". A static nested class is just one which doesn't implicitly have a reference to an instance of the outer class.

Static nested classes can have instance methods and static methods.

There's no such thing as a top-level static class in Java.

Side note:

main method is static since it must be be accessible for an application to run before any instantiation takes place.

final keyword is used in several different contexts to define an entity which cannot later be changed.

  • A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.

  • A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

  • A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a blank final variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.

Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.

When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. Once it has been assigned, the value of the final variable cannot change.

这篇关于静态和最终的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆