Can a final variable be initialized when an object is created?

前端 未结 8 749
夕颜
夕颜 2020-12-09 10:29

how it can be possible that we can initialize the final variable of the class at the creation time of the object ?

Anybody can explain it how is it possible ? ...<

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 11:08

    If you mean a static final member you can use a static initializer:

    class Example {
      public final static Map C;
    
      static {
        C = new HashMap<>();
        C.put("hi", 5);
      }
    }
    

提交回复
热议问题