0x00 overview
This paper mainly involves the object-oriented knowledge of Java, including classes and objects, object memory, member variables and local variables, encapsulation and construction methods.
0x01 classes and objects
1.1 understanding of classes and objects
Objective food is an object, so we often say that everything is an object.
- class
- Class understanding
- Class is an abstraction of a class of things with common attributes and behaviors in real life
- A class is the data type of an object. A class is a collection of objects with the same properties and behavior
- Simple understanding: class is a description of real things
- Class understanding
-
- Composition of classes
- Attribute: only the characteristics of things, such as mobile phone things (brand, price, size)
- Behavior: operations that can only be performed by things, such as mobile phone things (making phone calls and sending text messages)
- Composition of classes
- Relationship between classes and objects
- Class: class is an abstraction of a class of things with common attributes and behaviors in real life, such as mobile phones
- Object: it is a real entity that can be seen and touched, such as oppo mobile phone S21pro
- Simple understanding: class is a description of things, object is a concrete thing, and object is the concretization of class
1.2 definition of class
Class is composed of attributes and behaviors
- Attribute: reflected in the class through member variables (variables outside the methods in the class)
- Behavior: reflected in the class through member methods (compared with the previous methods, just remove the static keyword)
Class definition steps:
- Define class
- Write the member variable of the class
- Write member methods of classes
public class Class name { // Member variable Data type of variable 1; Data type of variable 2; ... // Member method Method 1; Method 2; ... }
Example
public class phone { // Member variable String brand; int price; // Member method public void call() { System.out.println("phone"); } public void sendMessage() { System.out.println("send message"); } }
1.3 use of objects
- Format for creating objects
Class name object name = new class name ();
- Format of calling member:
Object name. Member variable
Object name. Member method ();
Example
public class phoneDemo { public static void main(String[] args) { // create object phone p = new phone(); // Using member variables System.out.println(p.brand); System.out.println(p.brand); p.brand = "HuaWei"; p.price = 4999; System.out.println(p.brand); System.out.println(p.price); // Using member methods p.call(); p.sendMessage(); } }
1.4 Student object - Exercise
Requirements: first define a student class, and then define a student test class. In the student test class, the use of member variables and member methods is completed through objects
analysis:
- Member variables: name, age
- Member method: study, do homework
Example
class Student { // Member variable String name; int age; // Member method public void study() { System.out.println("study hard and make progress every day"); } public void doHomework() { System.out.println("The keyboard is broken, and the monthly salary is more than 10000"); } }
/* Student testing */ public class StudentDemo { public static void main(String[] args) { // create object Student s = new Student(); // Use object System.out.println(s.name + ", " + s.age); s.name = "Lin Qingxia"; s.age = 30; System.out.println(s.name + ", " + s.age); s.study(); s.doHomework(); } }
0x02 object memory diagram
2.1 single object memory diagram
- Member variable usage process

- Member method call procedure

2.2 multiple object memory diagram
Member variable usage process

- Member method call procedure

- Summary:
Multiple objects have different memory partitions in heap memory. Member variables are stored in their respective memory areas. Multiple objects of member methods share a member variable
2.3 multiple objects point to the same memory

- Summary:
When references to multiple objects point to the same memory space (the address values recorded by variables are the same)
As long as any object modifies the data in memory, then, no matter which object is used for data acquisition, it is the modified data.
0x03 member and local variables
3.1 differences between member variables and local variables
- Different positions in the class: the member variable is outside the method in the class, and the local variable is inside the method or on the method description
- Different locations in memory: member variables are in heap memory and local variables are in stack memory
- Different life cycles: member variables exist with the existence of the object, disappear with the disappearance of the object, and are bound to the object; Local variables exist with the method call, disappear with the method call, and are bound to the method
- Different initialization values: member variables have default initialization values, while local variables have no default initialization values. They must be defined before assignment
0x04 package
4.1 private keyword
private is a modifier that can be used to modify member variables and member methods
Members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, corresponding operations need to be provided
- Provide the get variable name () method. The user obtains the value of the member variable. The method is decorated with public
- The set variable name () method is provided to set the value of the member variable. The method is decorated with public
Example
class Student1 { // Member variable String name; private int age; // Provide get/set methods public void setAge(int a) { if (a < 0 || a > 120) { System.out.println("Incorrect age entered"); } else { age = a; } } public int getAge() { return age; } // Member method public void show() { System.out.println(name + ", " + age); } }
public class StudentDemo1 { public static void main(String[] args) { // create object Student1 s = new Student1(); // Assign values to member variables s.name = "Lin Qingxia"; s.setAge(30); // Call the show method s.show(); } }
4.2 use of private
Requirements: define standard student classes, require name and age to be decorated with private, provide set and get methods and show methods convenient for real data, create objects in the test class and use them, and finally output Lin Qingxia 30 on the console
Example
class Student2 { // Member variable private String name; private int age; // get set method public void setName(String n) { name = n; } public String getName() { return name; } public void setAge(int a) { age = a; } public int getAge() { return age; } public void show() { System.out.println(name + "," + age); } }
public class StudentDemo2 { public static void main(String[] args) { // create object Student2 s2 = new Student2(); // Assign values to member variables using the set method s2.setName("Lin Qingxia"); s2.setAge(30); s2.show(); // Use the get method to get the value of the member variable System.out.println(s2.getName() + ", " + s2.getAge()); } }
4.3 this keyword
this modified variable is used to refer to member variables. Its main function is to distinguish between local variables and member variables
- If the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter, not the member variable
- The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable
public class Student3 { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void show() { System.out.println(name + ", " + age); } }
4.4 this memory principle
This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.
Example:
public class StudentDemo { public static void main(String[] args) { Student s1 = new Student(); s1.setname("Lin Qingxia"); Student s2 = new Student(); s2.setname("Zhang Manyu"); } }
graphic


4.5 packaging ideas
- Encapsulation overview: encapsulation is one of the three characteristics of object-oriented (encapsulation, inheritance and polymorphism). It is the simulation of the objective world by object-oriented programming language. In the objective world, member variables are hidden inside the object, and the outside world cannot be operated directly
- Encapsulation principle: encapsulation is to hide some information of the class inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class. The member variable is private, and the corresponding get() / set() methods are provided
- Encapsulation benefits: controlling the operation of member variables through methods improves the security of the code, encapsulates the code with methods, and provides the reusability of the code
0x05 construction method
5.1 overview of construction method
Construction method is a special method
Function: create object Student stu = new Student();
Format:
public class Class name { Modifier class name(parameter) { } }
Function: it mainly completes the initialization of object data
Example:
class Student4 { private String name; private int age; // Construction method public Student4() { System.out.println("Nonparametric construction method"); } public void show() { System.out.println(name + ", " + age); } }
public class StudentDemo4 { public static void main(String[] args) { // create object Student4 s4 = new Student4(); s4.show(); } }
5.2 precautions for construction method
- Creation of construction method
If no construction method is defined, the system will give a default parameterless construction method; If a construction method is defined, the system will no longer provide the default construction method
- Overloading of construction methods
If you define a construction method with parameters and use a parameterless construction method, you must write a parameterless construction method
- Recommended usage
Whether used or not, the parameterless construction method is written manually
- Important functions
You can initialize member variables using a parameterized construct
Example
class Student5 { private String name; private int age; // Define your own parameterless construction method public Student5() { } public Student5(String name) { this.name = name; } public Student5(int age) { this.age = age; } public Student5(String name, int age) { this.name = name; this.age = age; } public void show() { System.out.println(name + ", " + age); } }
public class StudentDemo5 { public static void main(String[] args) { // create object Student5 s51 = new Student5(); s51.show(); System.out.println("-------------------"); // public Student5(String name) Student5 s52 = new Student5("Lin Qingxia"); s52.show(); System.out.println("-------------------"); // public Student5(int age) Student5 s53 = new Student5(30); s53.show(); System.out.println("-------------------"); // public Student5(String name, int age) Student5 s54 = new Student5("Lin Qingxia", 30); s54.show(); System.out.println("-------------------"); } }
5.3 standard production
Requirements: define a standard student class, which requires the creation of objects using null parameter and parametric construction methods respectively. The objects created with null parameters are copied through the set() method, the objects created with parameters are assigned directly, and the data is printed on the console through the show() method
Example:
class Student6 { // Member variable private String name; private int age; // Construction method public Student6() { } public Student6(String name, int age) { this.name = name; this.age = age; } // Member method public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void show() { System.out.println(name + ", " + age); } }
/* There are two ways to create an object and assign values to its member variables 1. After the object is created by the parameterless construction method, the set() method is used for assignment 2. Use the construction method with parameters to directly create objects with attribute values */ public class StudentDemo6 { // After the object is created by the parameterless construction method, the set() method is used for assignment public static void main(String[] args) { Student6 s61 = new Student6(); s61.setName("Lin Qingxia"); s61.setAge(30); s61.show(); System.out.println("----------------------"); // Use the construction method with parameters to directly create objects with attribute values Student6 s62 = new Student6("Lin Qingxia", 30); s62.show(); } }