Java basic modifiers

1. Package (package)

  1. Package: It's actually a folder
  2. Function: classify and manage classes
  3. Package definition format: package package name (multi-level package names are separated by .)
  4. Import package: When using classes under different packages, you need to write the full path of the class to simplify, use import
  5. Import package format: import package name; such as: import com.Demo.a1; or import com.Demo.*;
  6. If it is under the same package, you do not need to import the package import

2. Status modifier

1. final (final state)

  1. The meaning of the final keyword: final, after it is modified, it can no longer be changed
  2. What can be modified by final: Modified classes, modified methods, variables
  3. Modified class:
  • Indicates that a class is the final class, indicating that this class cannot have subclasses (cannot be inherited by other classes)
  • If a class is finalized, all methods in this class cannot be overridden
  1. Modified method:
    If a method is final modified, this method cannot be overridden
  2. Modified variable:
  • If a variable is modified by final, it will become a constant and cannot be assigned again, only once
  • Can be defined and assigned at the same time; such as: final int a = 10
  • Definition and assignment are separated, but can only be assigned once; final int b; b = 10
  • Variables defined using final are called: constants and constant names should use pure uppercase letters and use underscores between multiple words: final int MAX_VAULE = 10; final int MIN = 1
  1. Modify local variables:
  • The final modified variable is the data value of the basic type and will not change
  • If the final-modified variable is a reference type, the address value will not change, but the content in the address can be changed.
  1. Code case:
public class Student {
    
    
    public int age = 21;
}
public class finalDemo {
    
    
    public static void main(String[] args) {
    
    
        //final修饰基本类型变量
        final int age = 30;//final int age;先修饰再赋值就不会报错
        //      age=100;
        System.out.println(age);//输出age为30
        //final修饰引用类型变量
        final Student s = new Student();//修饰的地址值不变
        s.age = 200;//但内容可以改变
        System.out.println(s);//输出地址值为@49e4cb85
        s.age = 300;
        System.out.println(s);//输出地址值还是没改变为@49e4cb85
        System.out.println(s.age);//输出s.age=300;
//        s=new Student();不能创建新的地址值
    }
}

Two. static (static)

  1. static keyword meaning: static; can modify member methods, member variables, member methods
  2. Static modification features: shared access by all objects of the class
  3. Code case:
public class Student {
    
    
    public int age;
    public String name;
    public static String university;//static被类的所有对象共享访问
    public void show() {
    
    
        System.out.println(age + "," + name + "," + university);
    }
}
public class StaticDemo {
    
    
    public static void main(String[] args) {
    
    
        //静态变量访问特点:一般直接通过类名调用也可以通过对象名调用
        //static被类的所有对象共享访问
        Student.university="qinghua";

        Student s = new Student();
        s.age = 20;
        s.name = "xiaojiang";
    //    s.university = "qinghua";
        s.show();
        Student s2 = new Student();
        s2.age = 21;
        s2.name = "xiaoning";
        //    s2.university="qinghua";
        s2.show();
       // s2.university = "武汉大学";则xiaoyun为武汉大学
        Student s3 = new Student();
        s3.age = 22;
        s3.name = "xiaoyun";
        s3.show();
        //输出20,xiaojiang,qinghua
        //21,xiaoning,qinghua
        //22,xiaoyun,qinghua
    }
}
  1. Static access features: In short, static member methods can only access static members
  • Non-static member methods:
    • Ability to access static member variables
    • Ability to access non-static member variables
    • Ability to access static member methods
    • Ability to access non-static member methods
  • Static member method:
    • Can access member variables, but not directly
    • Can access member methods, but not directly
  1. Code case:

public class Student2 {
    
    
    //非静态成员变量
    public String name = "xiaojiang";
    //静态成员变量
    public static String university = "beida";

    //非静态成员方法
    public void show1() {
    
    
        System.out.println(name);
        System.out.println(university);
        show2(); //非静态都可以访问静态的和非静态的成员方法和成员变量
        show3();
    }
    public void show2() {
    
    

    }
    //静态成员方法
    public static void show3() {
    
    
//        System.out.println(name);
        System.out.println(university);
//        show1();
//        show2();
        show3();//静态只能静态的成员变量,成员方法
        show4();
    }
    public static void show4() {
    
    

    }
}
  1. Static application:
public class Student {
    
    
    //非静态成员属性
    public int age;
    //静态成员属性
    public static String name;

    //非静态成员方法
    public void show1() {
    
    
        System.out.println("非静态show1成员方法");
    }

    //静态成员方法
    public static void show2() {
    
    
        System.out.println("静态show2成员方法");
    }
}
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Student s = new Student();
        //通过创建对象调用
        s.show1();
        s.age = 20;
        System.out.println(s.age);
        //还可以通过类名来调用;静态调用静态的成员属性和方法
        //静态的内容既可以使用对象名访问,也可以使用类名访问.但是最好使用类名来访问.
        Student.show2();
        Student.name = "xiaojiang";
        System.out.println(Student.name);
    /*输出  非静态show1成员方法
           20
          静态show2成员方法
          xiaojiang  */
    }
}
  1. Static Application Scenario Constant Constant in Java:
public class Constant {
    
    
    //常量 不可以被改变的量(final)
    //常量名称全部都为大写  crtl+shift+u 点击常量可调整大小写
    //常量访问可以通过类名.
    //CODE_10== 成功
    public final static int CODE_10 = 10;
    //CODE_20== 错误
    public final static int CODE_20 = 20;
    //CODE_30== 找不到
    public final static int CODE_30 = 30;
}
public class ConstantDemo {
    
    
    public static void main(String[] args) {
    
    
        //    Constant constant = new Constant();
        //    constant.CODE=300;
        int code = 10;
        if (Constant.CODE_10 == code) {
    
    
            System.out.println("成功");
            return;
        }
        if (Constant.CODE_20 == code) {
    
    
            System.out.println("错误");
            return;
        }
        if (Constant.CODE_30 == code) {
    
    
            System.out.println("找不到");
            return;
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_62991876/article/details/123426156