Method call problem, value transfer, reference transfer and knowledge point supplement of classes in java (instantiation and constructor)

Method call classification:

// Static method static
call: class name. method name();
// non-static method does not have static
call: you need to instantiate this class first (new keyword) ---- instantiation format: object type object name = object Value; (example Student s=new Student())
call method: object name. method();

Pass by value (mostly performed in Java)

public class Demo1 {
    
    
        public static void main(String[] args) {
    
    
                int b =20;
                change(b);// 实参  实际上的参数
                System.out.println(b);
        }
        public static void change(int a){
    
    //形参  形式上的参数
                a=100;
        }
}

The change of the formal parameters in the value transfer does not affect the actual parameters. For example, in the above code, because there is no return value in the method, the formal parameter is assigned a value of 100, and then it returns to the main method, and b is also 20
(the process can be viewed with a breakpoint)

Pass by reference (the most important thing is to understand memory and objects)

Pass an object, the essence is to pass by value

Supplement to the class (instancing)

**Class supplement: a project should only have one main method.
Class: abstract, needs to be instantiated. After the
class is instantiated, it will return an object of its own!
student object is a specific example of a class Student
Student new new S = Student;
Student new new S1 = Student;
topic: Good Science Program? Can better model the world

The essence of object-oriented:

Organize code in a class, and organize (encapsulate) data in an object

Constructor (shortcut key alt+insert–dele on the right) automatically generates constructor (optional constructor without parameter / constructor with additional parameters)

Also called the construction method
1. Same as the class name
2. No return value
Function: 1. Use the new keyword, which is essentially
used to call the constructor 2. It is used to initialize the value.
Note: After defining the parameter structure, if you want to use none Parameter structure, also need to define a non-parameter structure

Guess you like

Origin blog.csdn.net/m0_52646273/article/details/114147317