Difference between a static and a final static variable in Java

后端 未结 6 2002
梦谈多话
梦谈多话 2020-12-22 18:09

Generally, final static members especially, variables (or static final of course, they can be used in either order without overlapping the meaning) are extensively used with

6条回答
  •  天涯浪人
    2020-12-22 18:30

    I'm unable to differentiate between a final and a final static member. The final static member is the one which is a static member declared as final or something else? In which particular situations should they be used specifically?

    Use a final static when you want it to be static. Use a final (non-static) when you don't want it to be static.

    A static variable or a final static variable can never be declared inside a method neither inside a static method nor inside an instance method. Why?

    Design decision. There's just no way to answer that without asking James Gosling.

    The following segment of code accordingly, will not be compiled and an compile-time error will be issued by the compiler, if an attempt is made to compile it.

    Because it violates the rule you just described.

提交回复
热议问题