java common keywords summary

Keywords: Java

super

  1. Invoking non-static attributes and methods of parent classes
  2. Call the constructor of the parent class (must be written on the first line of the subclass constructor)
  3. In the subclass construction method, we must call the parent class construction method. We can specify which construction method of the parent class is called by super. If not specified, we can call the parent class's parametric construction method automatically when we instantiate the subclass.

    class Test02 {
    Test02(){
        System.out.println("Parametric-free Construction of Parent Classes");
    }
    }
    public class Test01 extends Test02 {
    Test01(int v){//Parametric-free Construction Method for Automatically Calling Parent Classes
        System.out.println("A Parametric Construction Method for Subclasses");
    }
    Test01(){
        System.out.println("A Parametric-free Construction Method for Subclasses");
    }
    public static void main(String[] args) {
        new Test01(5);
        new Test01();
    }
    }

    Operation results:

  4. If the parent class does not have a parametric constructor, the child class constructor must manually call the parent class parametric constructor through super, otherwise compilation errors will be reported.
class Test02 {
    Test02(int v){
        System.out.println("Parametric Construction of Parent Classes");
    }
}
public class Test01 extends Test02 {
    Test01(int v){
        super(v);
        System.out.println("A Parametric Construction Method for Subclasses");
    }
    Test01(){
        super(7);//If you leave out Test01(), it will flush red.
        System.out.println("A Parametric-free Construction Method for Subclasses");
    }
    public static void main(String[] args) {
        new Test01(5);
        new Test01();
    }
}

this

  1. Accessing member variables of this class to resolve the name conflict between member variables and local variables
  2. Call member methods
  3. The invocation method can only be called in the construction method, and must be called in the first row.
    ps: Determine which constructor to call based on the list of parameters
public class Test01 {

    private int id;

    Test01(){
        System.out.println("Parametric-free construction method");
        this.method();
    }

    Test01(int a,int b){
        this(a);
        System.out.println("Parametric construction method 2:"+a+","+b);
    }

    Test01(int id){
        this();
        this.id = id;
        System.out.println("Parametric construction method 1:"+id);
    }

    public void method(){
        System.out.println("Method 1");
    }

    public static void main(String[] args) {
        new Test01(10,20);
    }
}

Operation results:

final

  1. Code level Top Level, Membership Level, Method Level
  2. final modified class=== cannot be inherited==
  3. Modified method=== cannot be overridden by subclasses==
  4. Modified variables are constants and can only be assigned once
  5. When a modified variable is a member variable, the virtual machine does not initialize it
class Test02{
    public final void  method(){//Final modification shows that this method is the final method and can not be overwritten.
        System.out.println("final Modification methods");
    }
}

public class Test01 extends Test02 {

    final int ID = 10;
    int num = 9;

//    public void method(){// will report an error:'method()'cannot override'method()' in'review.Test02'; the rewriting method is final
//
//    }

    public static void main(String[] args) {
//         new Test01().id = 100; // Error reporting: unable to assign value to final variable'id'
        System.out.println(new Test01().num);//9
    }
}

abstract

  1. Code level Top level, member level
  2. Classes containing abstract methods must be declared abstract classes
  3. Abstract classes cannot be instantiated directly, but only through subclasses
  4. The abstract method must be overridden by subclasses, so the == qualifier cannot be private==
  5. == The abstract method can not be modified by static ==, if it is modified by static, then we can call it directly by class name, and the abstract method has no subject at all, no business logic, so it is meaningless.
  6. == abstract classes cannot be modified with final==
abstract class Test02{

    public abstract void method();

    public void method2(){
        this.method();//this here represents a class that inherits the class, not the class itself.
        System.out.println("There can be common methods within abstract classes");
    }
}

public class Test01 extends Test02 {

    @Override
    public void method() {
        System.out.println("An abstract method for implementing abstract classes");
    }
    public static void main(String[] args) {
       Test02 test02 = new Test01();
       test02.method();
       System.out.println("===========");
       test02.method2();
    }

}

Operation results:

static

  1. Modifying member variables, member methods, internal classes, code blocks
  2. Code level Membership level
  3. Unbinding with objects: Non-static attributes and methods belong to the object of the class, static attributes and methods and objects unbound, belong to the class!!
  4. Access mode:
    Class name. Static properties / methods (recommended)
    Object names. Static properties / methods
  5. Static methods can only access attributes and methods modified with static (attributes and methods not modified with static need to create objects for re-access). Non-static methods can access static methods and attributes
  6. this keyword cannot appear in static method!!!
  7. static-modified code blocks and member variables are executed at class loading time! The order of execution is in writing order.
  8. static can only modify internal classes!!!
    Reason: static implementation and object unbundling, external classes do not belong to the object, how to unbundle it!
class Test02 {
    Test02(int v){
        System.out.println("Parametric Construction of Parent Classes");
    }
}
public class Test01 extends Test02 {

    static class Inner{
        Inner(int v){
            System.out.println("Construction Method of Static Internal Classes");
        }
    }

    class Inner2{
        Inner2(int c){
            System.out.println("Construction Method of Common Internal Classes");
        }
    }

    static int id;

    Test01(){
        super(7);
        System.out.println("A Parametric-free Construction Method for Subclasses");
    }

    static void mathod(){
        System.out.println("Static methods for subclasses");

    }

    public static void main(String[] args) {
        int a = Test01.id;
        Test01.mathod();
        System.out.println("===========A gorgeous partition line.===========");
        Test01 test = new Test01();
        int b = test.id;//Legitimate but not recommended
        test.mathod();//Legitimate but not recommended
        System.out.println("===========A gorgeous partition line.===========");
        Test01.Inner2 inner2 = new Test01().new Inner2(1);//Calls to common internal classes
        Test01.Inner inner = new Test01.Inner(4);//Invocation of static internal classes

    }
}

Operation results:

Supplement:

class Test1004{
    public static Test1004 a = new Test1004();
    public static Test1004 b = new Test1004();
    {
        System.out.println("Construct code blocks");
    }
    static{
        System.out.println("Static code block");
    }
}

public class StaticDemo {
    public static void main(String[] args) {
       Test1004 t = new Test1004();
    }
}

Operation results:

Access Qualifier

  • private (Class Access Level): Decorate a member of a class, then that member can only be decorated by other members of that class, and other classes cannot access it directly.
  • Default (package access level): If a class or member does not declare an access qualifier, it defaults to that access qualifier. Members or classes modified by this access qualifier can only be accessed by other classes under this package
  • protested (subclass access level): Members modified by this modifier can be accessed by other classes under the same package or by subclasses of that class under different packages
  • Public (Public Access Level): A class or member modified by public can be accessed by all classes.
Access Qualifier Intra class Other classes of the same package Subclasses of other packages Other classes of other packages
private Y N N N
default Y Y N N
protested Y Y Y N
public Y Y Y Y

Posted by mtrp on Mon, 14 Oct 2019 06:28:06 -0700