java keywords -- new, this, static

Keywords: Java Mobile

java keyword, also called reserved word (50), is an identifier with special meaning in java. It cannot be used as parameter name, variable name, method name, class name, package name, etc.

Now let's talk about the use of three keywords~~~

1, new keyword

1. Purpose: to create a new class object

2. Working mechanism:

  • Allocate memory space for object members and specify default values
  • Explicit initialization of member variables
  • Execution construction method
  • Calculate and return reference value

2, this keyword

1. Essence: refers to the address of the created object

2. Classification:

  • Normal method: this points to the object calling the method
  • Constructor: this points to the object being initialized

3. purpose:

  • Pointing object
  • Call overloaded constructor

[note]: this cannot be used in static method

/*
 * @Author: bpf
 * @Description: Test this keyword
 * @FilePath: \Learn in the Internet\Code\java\Test\TestThis.java
 */

public class TestThis {
    int index;
    int id;
    String name;
    String s = "Test <this> key word!";

    TestThis() {
        System.out.println(s);
    }
    TestThis(int index, int id) {
        // TestThis();      // Unsuccessful call to constructor
        this();             // Call the parameterless constructor
        // index = index;   // This sentence is meaningless
        this.index = index;
        this.id = id;
    }
    TestThis(int index, int id, String name) {
        this(index, id);    // Use this Must be the first sentence when calling a constructor
        this.name = name;
    }

    public void showInfo() {
        System.out.printf(  // This sentence does not add. this Same effect
            "%d - %d: %s\n", this.index, this.id, this.name);
    }

    public static void main(String args[]) {
        TestThis test1 = new TestThis(0, 10086, "China Mobile");
        test1.showInfo();
        TestThis test2 = new TestThis(1, 10010, "China Unicom");
        test2.showInfo();
    }
}

 

 

 

3, static keyword

1. Purpose:

  • Modify variables in a class to become member variables, that is, class variables
  • Modify a method in a class to become a static method, that is, a class method

2. characteristics:

  • For all objects of the same class, there is only one member variable shared by all objects of the class.
  • static cannot modify local variables in instance methods
  • Static decorated methods cannot call non static members
public class TestStatic {
    int index;
    String name;
    static String school = "Underwater School"; // Class variables

    public TestStatic(int index, String name) {
        this.index = index;
        this.name = name;
    }

    public void welcome(int index, String name) {
        printSchool();
        System.out.printf("Welcome NO.%d %s to Underwater School!\n", index, name);
    }

    public static void printSchool() {          // Class method
        // welcome(this.index, this.name);  
            // Static and non static variables cannot be called in static methods.
            // Cannot be used in static methods this Keyword
        System.out.println(school);
    }

    public static void main(String args[]) {
        TestStatic stu = new TestStatic(1, "Jerry");
        stu.printSchool();
        stu.school = "Sky School";
        stu.printSchool();
    }
}

Posted by hukadeeze on Thu, 09 Apr 2020 08:00:51 -0700