static 与final abstract关键字

一、通常访问类的属性与方法需要创建该类的实例,而static关键字的访问不需要某个特定的实例。

1.静态变量

使用类名.变量直接访问

package text5;
public class Father{
    private static int x=5;
    public static void main(String[] args) {
        Father.x++;
        Father v=new Father();
        v.x++;
        System.out.println(x);
    }
}
View Code

2.静态方法

package text5;
public class Father{
    private static int x=5;
    public static void main(String[] args) {
        Father.x++;
        Father v=new Father();
        v.x++;
        System.out.println(x);
        Father.test();
    }
    public static void test(){
        System.out.println("test..x..");
    }
}
View Code

3.静态代码块

package text5;
public class Father{
    static{
        System.out.println("静态代码块");
    }
    private static int x=5;
    public static void main(String[] args) {
        Father.x++;
        Father v=new Father();
        v.x++;
        System.out.println(x);
        Father.test();
        Father v1=new Father();
    }
    public static void test(){
        System.out.println("test..x..");
    }
}
View Code

二、final关键字

final关键字是最终的意思,使用final修饰类,代表该类是不能被继承的。final修饰方法表示该方法不能够被改写,final修饰变量表示该常量

三、abstract关键字

猜你喜欢

转载自www.cnblogs.com/helloworld2019/p/10696965.html