Methods in Java (just read this article)

Classification:
In Java, methods can be divided into the following categories:

  1. Instance Method :

    • Instance methods belong to instances of a class and can access the instance variables and instance methods of the class.
    • The keyword can be used inside the method thisto refer to the current object.
    • Instance methods can be called on objects.
  2. Static Method :

    • Static methods belong to the class itself, not to instances of the class.
    • Instance variables and instance methods cannot be accessed because they are associated with the object.
    • Static methods can be called directly through the class name.
  3. Constructor :

    • A constructor is a special type of method used to initialize an object when it is created.
    • The constructor has the same name as the class and has no return type, including void.
    • Automatically called when creating an object.
  4. Abstract Method :

    • Abstract methods do not have a method body, only the declaration of the method, which is abstractmodified with the keyword.
    • It must be implemented by subclasses and is used to force subclasses to provide specific behavior.
  5. Final Method :

    • Final methods cannot be overridden or overridden by subclasses.
  6. Synchronized Method :

    • Synchronization methods are used to implement thread synchronization and can only be accessed by one thread.
  7. Native Method :

    • Native methods are methods implemented in non-Java languages ​​(such as C or C++) that interact with the underlying operating system and hardware.
  8. Varargs Method :

    • Variadic methods allow methods to accept a variable number of arguments.
  9. Recursive Method :

    • Recursive methods are methods that call themselves.
  10. Method Overloading :

    • Method overloading means that in the same class, multiple methods with the same name can be defined, but their parameter lists must be different (type, number, order of parameters).

The above are the common classifications of methods in Java. Each type of method has its specific purpose and rules.

Constructor method:
In Java, a constructor method is a special method of a class that is used to initialize the instance variables of an object and perform other necessary operations so that the object can work correctly. The constructor has the same name as the class, has no return type, and is automatically called when the object is created.

The following are some basic characteristics of constructors:

  1. Method name is the same as the class name : The name of the constructor method must be exactly the same as the class name.

  2. No return type declaration : Constructors do not need to explicitly declare return types, including void.

  3. Cannot be called explicitly : The constructor is automatically called when the object is created and cannot be called explicitly.

  4. Can have parameters : The constructor can accept parameters to assign initial values ​​to the instance variables of the object.

  5. Default constructor : If no constructor is defined in the class, Java will provide a default no-argument constructor.

Here's a simple example:

public class Person {
    
    
    String name;
    int age;

    // 构造方法
    public Person(String n, int a) {
    
    
        name = n;
        age = a;
    }

    public void display() {
    
    
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
    
    
        // 创建Person对象,调用构造方法
        Person person1 = new Person("John Doe", 30);
        person1.display(); // 输出: Name: John Doe, Age: 30

        // 创建另一个Person对象,调用构造方法
        Person person2 = new Person("Jane Doe", 25);
        person2.display(); // 输出: Name: Jane Doe, Age: 25
    }
}

In the above example, Personthe class has a constructor that accepts two parameters nand ato initialize the nameand ageproperties. PersonThis constructor is automatically called when an object is created .

The constructor is a very important part of Java object-oriented programming. It is used to ensure that the initialization process of the object is reasonable and accurate, thereby ensuring the correct use of the object.

Guess you like

Origin blog.csdn.net/yang_guang3/article/details/133281018