状态修饰符:final、static

1、final(最终态):可以修饰成员变量,成员方法,类

      特点:1)修饰变量时,必须初始化,且该变量的值将不能被修改

                2)修饰方法时,该方法可以重载,不能重写,可以继承

                3)修饰类时,该类不能被继承

        总结:final修饰,不能被修改,重写,(被类)继承

1)修饰变量,需初始化,不能修改
class Student1{
    final String name="zs";
    
    //final修饰的变量name不可修改,必须初始化
    //业内规则,fianl修饰的变量名称大写(做区分,方便实用)
    public void change(){
       name="ls";//报错
    }
}
2)修饰方法,不能重写,可以被重载
class Person1{
    public final void show(){
        System.out.println("方法不能重写");
    }
}
class Teacher extends Person1{
    public void show(){}  //报错
}
3)修饰方法,可被继承
class Person1{
    public final void show(){
        System.out.println("方法能被继承");
    }
}
class Teacher extends Person1{
    //测试类
    public void test(){
        show();
    }
}
4)修饰类,不能继承
final class Person1{
    public  void show(){
        System.out.println("修饰类时不能被继承");
    }
}
class Teacher extends Person1{//报错
}

  注意:修饰引用数据类型时,是地址值不能改变,地址所标识空间中的内容可以改变

final int[] a={1,2,3,4,5,6,7,8,9};
System.out.println(a[0]);//1
a[0]=100;
//fianl修饰引用数据类型时,可以更改地址值对应空间的内容a[0]
System.out.println(a[0]);//100

//引用数据类型都会在内存中开辟空间
//final修饰引用数据类型时,不可更改地址值
a=new int[5];//报错

final String name="zs";
name="ls";//报错

2、static(静态):可以修饰成员变量,成员方法

      特点 :1)被类的所有对象共享

                 2)也是判断是否使用static的条件

                 3)修饰的成员变量,成员方法可以通过类名直接调用

        注意:静态成员方法只能访问static修饰的成员变量和成员方法,因为static修饰的成员是在                       类加载时而加载

class Student1 {
    public void show() {
        System.out.println("show");
    }
}

public class Test {
    public static void main(String[] args) {
        //当多个对象使用的值为同一个时
        //如何只赋值一次,就可完成要求
        Student1 s = new Student1();
        play();
//       show();//报错,不是static修饰不能随类的加载而加载,需要通过类名s.show();调用
    }

    public static void play() {
        System.out.println("play");
    }
}


class Student1 {
    String name ;
    static String clazz;//static修饰的成员变量只定义一次
}

public class Test {
    public static void main(String[] args) {
        //类加载在栈中,new在堆中
        Student1 s1 = new Student1();
        s1.name="zs";
        s1.clazz="十三期";
        System.out.println(s1.name+"---"+s1.clazz);

        Student1 s2 = new Student1();
        s2.name="ls";
        System.out.println(s2.name+"---"+s2.clazz);

    }
}

class Student1 {
    String name;
    static String clazz;
}

public class Test {
    public static void main(String[] args) {
        //所表示的学生都是班级十三期
        Student1 s = new Student1();
        s.clazz = "十三期";
        
        //在new Student之前给定班级
        //Student类已经加载了,类只会加载一次
        Student1.clazz = "十三期";
        System.out.println(s.clazz);
    }
}

class Demo1 {
    //先走定义变量,然后代码块,然后赋值
    static {
        i=20;
    }
    static int i=10;
    public void show(){
        System.out.println(i);
    }
}

public class Test {
    public static void main(String[] args) {
        Demo1 d = new Demo1();
        d.show();//10

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值