static修饰属性:无论类生产多少个对象,所以这些对象共同使用唯一一份静态的成员变量;如果一个对象对这个静态属性进行改变,则其他的该属性也随着改变;我们可以使用(类名.成员变量名)的方式来使用它(推荐使用);
static修饰方法:static修饰方法叫做静态方法。
eg:
public class StaticTest
{
public static void main(String[] args)
{
Animal animal = new Cat();
anmial.output();
Cat cat = new Cat();
cat.output();
}
}
class Animal
{
public static void output()
{
System.out.println("The animal");
}
}
class Cat extends Animal
{
public static void output()//隐藏方法
{
System.out.println("The cat");
}
}
输出结果:
The animal
The cat
----static关键字修饰方法可以继承但不可以重写。在Cat类中的void ouput() 写法是hidden 类Animal的方法,隐藏与重写的区别是隐藏方法的调用取决于调用该方法的类,而重写是调用子类的方法。
final关键字
final修饰属性时注意
1、赋初值(声明时赋值,在所有的构造函数中赋值)
2、不可改变其值