Difference between final variables and compile time constant

后端 未结 5 1527
孤城傲影
孤城傲影 2021-01-31 03:59

What is the difference between final variables and compile time constants?

Consider the following code

final int a = 5;
final int b;
b=6;
int x=0;
switch         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 04:35

    The problem is, that all case: statements must be ultimate at compile time. Your first statement is ultimate. a will for 100% be no other value than 5.

    final int a = 5;
    

    However, this is not guaranteed for b. What if there would be an if-statement around b?

    final int b;
    if(something())
       b=6;
    else
       b=5;
    

提交回复
热议问题