Difference between a static and a final static variable in Java

后端 未结 6 2019
梦谈多话
梦谈多话 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:23

    final keyword simply means "this cannot be changed".It can be used with both fields and variables in a method.When a variable is declared final an attempt to change the variable will result to a compile-time error.For example if i declare a variable as final int x = 12; trying to increment x that is (++x) will produce an error.In short with primitives final makes a value a constant. On the other hand static can only be applied with fields but not in methods.A field that is final static has only one piece of storage.final shows that it is a constant(cannot be changed), static shows it is only one.

提交回复
热议问题