point of having an instance variable as final?

前端 未结 6 425
醉梦人生
醉梦人生 2021-02-01 23:25

what is the point of having an instance variable as final?

isn´t it better to have that variable set as a static final variable then?

cause if it can\'t be chang

6条回答
  •  情话喂你
    2021-02-01 23:43

    class A{
        public final int x;
    
        A(int arg){
            x = arg;  // This is legal  !!!
        }
    }
    
    public class HelloWorld{
    
         public static void main(String []args){
             A  a = new A(1);
             A  b = new A(2);
             System.out.printf("a.x = %d, b.x = %d", a.x, b.x);
         }
    }
    

提交回复
热议问题