Today we learned about classes and objects, where the use of the This keyword, static variables and static methods, and reference delivery require special attention.
The first is the reference pass:
In this procedure, you can change the properties of the class object by external assignment, or you can modify the properties of the class object by external methods. The final result is:
It is important to note that if you change the type of temp to string, the result is still changed. String cannot be modified within the heap space, but within the class object, each modification of the property will open up new space again, and if the string is modified only within the main method, its output will not change:
Here's how to use the This keyword:
1. Represents a property in a class and invokes a method.
2. Call the constructor method in this class.
3. Represents the current object.
For example, in the construction method
Public people (String Name,int age) {
THIS.name = name;
This.age = age;
}
This method can be used to achieve the purpose of the assignment, this to represent the properties in this class, no assignment is possible without the this keyword.
The constructor method in this class can be called with the This keyword.
Add a parameterless construction method based on the above construction method.
Public people () {
System.out.println ("parameterless constructor");
}
To construct a method that calls this parameterless constructor, the original construction method needs to be rewritten as:
Public people (String Name,int age) {
This (); Be sure to write the call to the beginning, or compile the error
THIS.name = name;
This.age = age;
}
The third purpose is to represent the current object:
Use of the Static keyword:
Members that are modified by static are called static members, not static modifiers called instance members.
The static variable is equivalent to a global variable, stored in a common storage unit, and the class must be independent of the method if it contains a static variable.
In the same way as static variables, the static method that is modified by static is part of a class:
1. A non-static method is a method that belongs to an object. The static method belongs to the entire class and is shared by all objects.
The 2.static method cannot manipulate and process members belonging to an object, can access only static member variables or call static member methods, and cannot access instance variables and instance methods.
3. This or super cannot be used in a static method.
4. Calling a static method is preferable to calling directly using the class name.
Java Self-study diary--partⅱ