public class Test{ public static int num1=1; public final int num2=2; }static属性是类属性 它属于一个类而不是某个对象 在类被加载进运行环境时创建 为这个类的所有对象所共享 即无论你创建多少个对象 这个属性都只会有一个 像上面num1,我们在类的外部可以通过Test.num1来引用 当然也可以(new Test()).num1 但是不推荐这样做 像Eclipse等会直接把这种写法算作错误final属性 在定义时就必须初始化 而且之后他的值是不允许改变的任何对他的赋值都是错误的 但是 如果没有static属性 final属性是对象属性 即创建一个对象就会创建一个final属性
static 静态的,属于一个类,可以直接被类调用而不用借助对象。 final 从字面上来看也可以看出,是最终的。即不可继承不可修改。 对于是不是公开的问题,还得看前面的访问修饰符(public or private)
final: Variable's value cannot be changed, Methods cannot be overridden, Classes cannot be subclassed. static: Belongs to the class, not to any particular instance of the class; For variables, there is only one copy for all instances of the class. if any instance changes the value, the other instances see that changes; For methods, it can be called without having created an instance,and cannot be used the "this" keyword.
public static int num1=1;
public final int num2=2;
}static属性是类属性 它属于一个类而不是某个对象 在类被加载进运行环境时创建 为这个类的所有对象所共享 即无论你创建多少个对象 这个属性都只会有一个
像上面num1,我们在类的外部可以通过Test.num1来引用 当然也可以(new Test()).num1 但是不推荐这样做 像Eclipse等会直接把这种写法算作错误final属性 在定义时就必须初始化 而且之后他的值是不允许改变的任何对他的赋值都是错误的 但是 如果没有static属性 final属性是对象属性 即创建一个对象就会创建一个final属性
final 最终的,不可继承不可修改的。
static对象属于类一级,而不属于对象级别
同一个类的对个副本中,static对象只存在一个,所有类副本共享这个static
static定义静态变量,在程序运行前加载到内存中。
final 从字面上来看也可以看出,是最终的。即不可继承不可修改。
对于是不是公开的问题,还得看前面的访问修饰符(public or private)
Variable's value cannot be changed,
Methods cannot be overridden,
Classes cannot be subclassed. static:
Belongs to the class, not to any particular instance of the class;
For variables, there is only one copy for all instances of the class. if any instance changes the value, the other instances see that changes;
For methods, it can be called without having created an instance,and cannot be used the "this" keyword.