在Java中,我们使用带有变量的final关键字来指定其不被更改的值。 但我看到你可以改变类的构造函数/方法的值。同样,如果变量是静态的,那么这是一个编译错误。

代码如下:

import java.util.ArrayList;
import java.util.List;

class Test {
  private final List foo;

  public Test()
  {
      foo = new ArrayList();
      foo.add("foo"); // Modification-1
  }
  public static void main(String[] args) 
  {
      Test t = new Test();
      t.foo.add("bar"); // Modification-2
      System.out.println("print - " + t.foo);
  }
}

以上代码工作正常,没有错误。

现在将变量更改为静态:

private static final List foo;

现在它是一个编译错误。期末考试是怎么进行的呢?


当前回答

Final关键字有很多种用法:

final类不能被子类化。 final方法不能被子类重写 final变量只能初始化一次

其他用途:

在方法体中定义匿名内部类时, 在该方法范围内声明为final的所有变量为 从内部类内部访问

静态类变量将从JVM开始时就存在,并且应该在类中初始化。如果您这样做,错误消息将不会出现。

其他回答

以下是使用final的不同上下文。

最终变量一个最终变量只能赋值一次。如果变量是引用,这意味着不能将该变量重新绑定到引用另一个对象。

class Main {
   public static void main(String args[]){
      final int i = 20;
      i = 30; //Compiler Error:cannot assign a value to final variable i twice
   }
}

Final变量可以稍后赋值(声明时不强制赋值),但只能赋一次。

Final类不能扩展(继承)

final class Base { }
class Derived extends Base { } //Compiler Error:cannot inherit from final Base

public class Main {
   public static void main(String args[]) {
   }
}

Final方法不能被子类覆盖。

//Error in following program as we are trying to override a final method.
class Base {
  public final void show() {
       System.out.println("Base::show() called");
    }
}     
class Derived extends Base {
    public void show() {  //Compiler Error: show() in Derived cannot override
       System.out.println("Derived::show() called");
    }
}     
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}

java中的final关键字用于限制用户。java final关键字可以在许多上下文中使用。Final可以是:

变量 方法 类

final关键字可以与变量一起应用,没有值的final变量称为空白final变量或未初始化final变量。它只能在构造函数中初始化。空白的final变量也可以是静态的,它只会在静态块中初始化。

Java final变量:

如果你让任何变量作为final,你不能改变final变量的值(它将是常量)。

final变量示例

有一个最终变量的速度限制,我们将改变这个变量的值,但它不能改变,因为最终变量一旦赋值就永远不能改变。

class Bike9{  
    final int speedlimit=90;//final variable  
    void run(){  
        speedlimit=400;  // this will make error
    }  

    public static void main(String args[]){  
    Bike9 obj=new  Bike9();  
    obj.run();  
    }  
}//end of class  

Java最终类:

如果将任何类设置为final类,则不能扩展它。

final类示例

final class Bike{}  

class Honda1 extends Bike{    //cannot inherit from final Bike,this will make error
  void run(){
      System.out.println("running safely with 100kmph");
   }  

  public static void main(String args[]){  
      Honda1 honda= new Honda();  
      honda.run();  
      }  
  }  

Java final方法:

如果您将任何方法设置为final,则不能重写它。

final方法示例 (Honda中的run()不能覆盖Bike中的run())

class Bike{  
  final void run(){System.out.println("running");}  
}  

class Honda extends Bike{  
   void run(){System.out.println("running safely with 100kmph");}  

   public static void main(String args[]){  
   Honda honda= new Honda();  
   honda.run();  
   }  
}  

共享: http://www.javatpoint.com/final-keyword

“最终变量只能赋值一次”

*反思* -“哇哦,等等,拿着我的啤酒”。


在两种情况下冻结final字段:

构造函数结束。 当反射设置字段的值时。(想要多少次就有多少次)


让我们犯法吧

public class HoldMyBeer 
{
    final int notSoFinal;
    
    public HoldMyBeer()
    {
       notSoFinal = 1;
    }

    static void holdIt(HoldMyBeer beer, int yetAnotherFinalValue) throws Exception
    {
       Class<HoldMyBeer> cl = HoldMyBeer.class;
       Field field = cl.getDeclaredField("notSoFinal");
       field.setAccessible(true);
       field.set(beer, yetAnotherFinalValue);
    }

    public static void main(String[] args) throws Exception 
    {
       HoldMyBeer beer = new HoldMyBeer();
       System.out.println(beer.notSoFinal);
       holdIt(beer, 50);
       System.out.println(beer.notSoFinal);
       holdIt(beer, 100);
       System.out.println(beer.notSoFinal);
       holdIt(beer, 666);
       System.out.println(beer.notSoFinal);
       holdIt(beer, 8888);
       System.out.println(beer.notSoFinal);
    }    
}

输出:

1
50
100
666
8888

“final”字段已经被分配了5个不同的“final”值(注意引号)。它可以一直被赋予不同的值……

为什么?因为反射就像chucknorris,如果它想改变一个初始化的final字段的值,它就会这样做。有人说他自己就是那个将新值推入堆栈的人:

Code:
   7: astore_1
  11: aload_1
  12: getfield                
  18: aload_1
  19: bipush        50        //wait what
  27: aload_1
  28: getfield                
  34: aload_1
  35: bipush        100       //come on...
  43: aload_1
  44: getfield                
  50: aload_1
  51: sipush        666      //...you were supposed to be final...
  60: aload_1
  61: getfield                
  67: aload_1
  68: sipush        8888     //ok i'm out whatever dude
  77: aload_1
  78: getfield                

由于final变量是非静态的,所以可以在构造函数中初始化它。但是如果你让它是静态的,它就不能被构造函数初始化(因为构造函数不是静态的)。 对列表的添加不期望通过使列表最终停止。Final只是将引用绑定到特定对象。你可以自由地改变对象的“状态”,但不能改变对象本身。

阅读所有答案。

在另一个用户案例中,final关键字可以用在方法参数中:

public void showCaseFinalArgumentVariable(final int someFinalInt){

   someFinalInt = 9; // won't compile as the argument is final

}

可用于不应更改的变量。