keywords in java

keywords in java


1. The static keyword

  • Role: used to modify members (member variables and member functions).

  • The modified members have the following characteristics:

    1. Loaded as classes are loaded.
    2. takes precedence over object existence.
    3. Shared by all objects.
    4. Can be called directly by the class name.
  • Precautions:
    1. Static methods can only access static members
    2. But non-static members can access static members;
    3. This and super keywords cannot be used in static methods
    4. In inheritance, methods whose parent class is static cannot be overridden.
  • Example:
    • Code:
class Student
{
    //随着类的加载而加载
    public static String name="李四";
    public static int age=20;
    public static void study()
    {
        System.out.println("Student study");
    }
}
class Demo 
{
    public static void main(String[] args) 
    {
        //被static关键字修饰的方法和成员变量能通过类名直接调用
        Student.study();
        System.out.println(Student.name);
        System.out.println(Student.age);
    }
}
  • Output result:
    write picture description here

Second, the private keyword

  • Features:

    • Permission modifiers, used to modify members (member variables and member functions).

    • Private is only a form of encapsulation, providing external access methods (set and get methods) to operate and judge data to improve security.

  • Precautions:

    • The privatized members are only valid in this class; in inheritance, the private methods of the parent class cannot be overridden , and the subclass cannot inherit the private content (methods and variables) of the parent class.

    • Private is just a manifestation of encapsulation.

    • After the constructor is made private (the execution body is empty), the object cannot be created, the method in the class is defined as static, and the class name is used to call it.

Three, this keyword

  • Definition : this represents a reference to the object to which the function it is located belongs to.

  • When using the this keyword:

    1. In a constructor, another constructor in the same class is called through the this keyword.

    2. In an instance method, the local variable or parameter has the same name as the instance variable, and the compiler cannot distinguish the variable. In this case, the this keyword is used.

    3. Within an instance method, a reference to the current instance is accessed through the this keyword.

  • Example:

    • code
public class Test
{
    String name;
    //局部变量与成员变量名称相同,用this关键字来区分
    Public Test(String name)
    {
        this.name  = name; 
    }
}

4. super keyword

  • Features:

    • super represents the identity of the memory space of the parent class.

    • In inheritance, in the constructor of the subclass, the first line has a default implicit statement: super(), so the constructor of the superclass will be executed first.

  • When using the super keyword:

    • When the member variables in the child parent class have the same name, use super to distinguish the parent class.

    • When you continue to use the overridden parent class method, you can super.函数名get it.

  • Precautions:

    • Because both super and this can only be defined on the first line, there can only be one.

Five, final keyword

  • Features:

    • final can modify classes, methods, and variables.

    • Final modified classes cannot be inherited.

    • Final modified methods cannot be overridden

    • A final-modified variable is a constant that can only be assigned a value once.

Six, abstract keyword

  • Features:

    • A class with an abstract method is inherently abstract and must be declared abstract.

    • abstract classes cannot be instantiated.

    • A subclass of the abstract class can only be instantiated if the subclass of the abstract class implements all the abstract methods of its superclass. Such classes are called concrete classes to distinguish them from abstract classes.

    • A subclass of an abstract class is also an abstract class if it does not implement all the abstract methods of its superclass.

    • This keyword cannot coexist with private, static, and final (cannot be overridden by subclass inheritance).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519043&siteId=291194637