Java中易混淆

来源:互联网 发布:mac 邮箱软件 编辑:程序博客网 时间:2024/05/16 05:59

1,instance variance vs. class variance , constant vs. static

local 
instance var: each object of that class has that a different version of that variable. eg: count1, count2, each have their own variables, their own box
class var: a variable shared by all objects in a class  eg: there's one variable counter , that all objects of the type of the class are referring to the one single box

specify:
instance var: private/ public and give a name
class variable: add "static", a static variable-----a static variable, eg: constant. PI should be shared as a class variable among all circles. We just want one it share, so we call static
constant is static
but static is not necessarily constant, they don't have to be final

eg:
/* create a class variable */
private static int COUNTER;

instance variable有很多copy
class variable只有一个copy 被share
区别在于有static
并且可以通过类名访问
constant实际上都是class vvativariable,除了static之外,还需要加上final 关键字。但是也可以定义constant instance variable和constant local variable

原创粉丝点击