Common Java keywords and their use

Keywords: Programming jvm

  1. final
  2. static
  3. this
  4. super (four usages)

1. Modify local variables (assignments can only be made once, and remain constant for life)

  • Modify basic data types: Modified data variable values are not mutable
  • Modify reference data types: address values are not mutable, content is mutable

Matters needing attention:

  1. 1 Error:
 public static void main(String[] args) {
        final int c = 0;
        for (int i=0;i<10;i++)
            c=i;                        //Error: Unable to assign value to final variable c

  1. 2 Correct:
 public static void main(String[] args) {
        for (int i=0;i<10;i++)
            final int c = 0;            //Correct: local variable, released each time

2. Modify member variables (data values of variables are not variable)

  • Member variables have default values, manual assignment is required after final modification
  • To assign final modified member variables by a construction method, it is necessary to ensure that all overloaded construction methods assign final modified member variables to ensure their initialization.

Choose one:

public class Demo {
    private final String name = "Sang Fengjiao";//Direct assignment
    public Demo(){}

public class Demo2 {
    private final String name ;
public Demo2()
    		name = "Sang Fengjiao";//Assignment by construction method
 public Demo2(String name) { = name;//Assignment by construction method

3. Modifiers

This class decorated with final cannot have any subclasses

Note: If a class is final-modified, all member methods cannot be overridden (because there are no subclasses)

  • A class modified by final whose member variables can be designed to be final according to their actual needs
  • Membership methods in final classes are implicitly specified as final methods

Modification Method

Method decorated with final cannot be overridden (cannot be override)


Static-modified content (loaded once with class loading; stored in a fixed memory area (static area), so called directly with class name; takes precedence over object existence, so it can be shared by all objects)

Modified member variables and member methods, where the modified members belong to a class, not just to an object, can be called directly using the class without creating the object

  • Class variable definition format: static int num = 10; (static data type variable name;)
  • Class method definition format: public static return value type method name (){}


  1. A member variable that is not modified by a static is called an instance variable, which belongs to an object
  2. Class method usage and its considerations:


  • Static-modified methods are static methods (class methods), which use class names. method name calls
  • Definition format: public static return value type method name (parameter list) {}

Be careful:

package com.day02;
 * @author SFJ
 * @date 2019/11/8
 * @time 22:19
public class Test1 {
    private String name="Sang Fengjiao";
    static String idCard ="3709211998092033**";
    public static void method1()
        method2();//Error, static methods cannot directly access non-static member methods
        new Test1().method2();//Correct, static methods create anonymous objects to access non-static member methods
        System.out.println(name);//Error, no direct access to non-static member variables
        System.out.println(idCard);//Correct, direct access to static member variables
    public void method2()
        System.out.println(idCard);//Correct, non-static methods can access static variables
        method1();//Correct, non-static methods can access static methods
    public static void main(String[] args) {
        Test1 test1 = new Test1();
  • Static methods do not have direct access to common variables and member methods and need to create objects to access them
  • Static methods have direct access to static methods and static member variables
  • Static methods and static member variables are accessible in nonstatic methods
  • super and this keywords cannot be used directly in static methods

Static code block:

  • Defined inside a class, outside any method
  • Define format: static {}

The primary purpose is to initialize class variables

3.this (represents a reference to the current object)

  • Distinguish between a function parameter and a member variable of the same name (this. member variable name = parameter - most commonly used)
  • In a construction method (this() or this (parameter list), only one construction method can be referenced and only in the first sentence)
  • Note: This cannot be used in static-modified methods, because static-modified methods belong to classes, while this refers to references to objects, conflicting
  • package com.day02;
     * @author SFJ
     * @date 2019/11/8
     * @time 22:53
    public class Test3 {
            private int i=0;
            Test3(int i) {//First constructor: has an int parameter
                this.i = i + 1;//this means referencing member variable I instead of function parameter I
                System.out.println("Int constructor i-this.i:  " + i + "-" + this.i);
                System.out.println("i-1:" + (i - 1) + "this.i+1:" + (this.i + 1));
                //The two output results fully demonstrate that I and this.i are different!
            Test3(String s){ //  Second constructor: has a String parameter
                System.out.println("String constructor:  "+s);
            Test3(int i,String s){//  The third constructor: has an int parameter and a String parameter
                this(s);//this calls the second constructor
                this.i=i++;//this to reference member variables of the class
                System.out.println("Int constructor:  "+i+"/n"+"String constructor:  "+s);
            public Test3 increment(){//increment
                return this;//Returns the current object belonging to (Test3)
            public static void main(String[] args){
                Test3 t0=new Test3(21);
                Test3 t1=new Test3("Sang Fengjiao");
                Test3 t2=new Test3(21,"Sang Fengjiao");
                //t0.increment() returns a Test3 object based on i+,
                //Next, return the Test3 object based on the object returned above!


4.super (refers to parent class)

  • When members of the same name (including variables and methods) exist in the child parent class, members of the child class are accessed by default in the child class, and members of the parent class can be specified by the super keyword.
  • When creating a subclass object, the parent class's parameterless construction method is invoked by default, and the parent class's construction method can be invoked by specifying the super keyword:
  1. jvm adds a super() statement to the construction method of a subclass if the construction method calling the parent class is not specified on the construction method of the subclass
  2. When the super keyword calls a parent constructor, the statement must be the first statement in the subclass constructor
  3. The super and this keywords cannot appear in the same constructor and call other constructors at the same time.Because both statements require the first statement
  4. In inheritance, as long as a subclass object is created, the default must first call the parent parameterless construction method to create the parent object
    package com.day02;
     * @author SFJ
     * @date 2019/11/8
     * @time 23:08
    public class Test4 {
        public static void main(String[] args) {
            Son s = new Son(3);
            System.out.println();  //4   
    class Father {
        int x = 1;
        Father() {
            System.out.println("Construction method of parent class without parameters");
        Father(int x) {
            this.x = x;
            System.out.println("Constructive method with parent class parameters");
        void speak() {
            System.out.println("I am a parent");
    class Son extends Father {
        int y = 1;
        Son() {
            System.out.println("Construction method of subclass parameterless");
        Son(int y) {
            this.y = y+x;
            System.out.println("Construction Method of Subclass with Parameters");
        void run() {
            super.speak(); //Functions that access the parent class
            System.out.println("I am a subclass");

Posted by anon on Fri, 08 Nov 2019 17:54:47 -0800