JavaSE learning notes - object oriented

1. Overview of object-oriented thought

(1) Object oriented is an idea, which is based on process oriented. That is to say, object-oriented is to realize functions through objects, encapsulate functions into objects, and let objects realize specific details; This idea takes the data as the first and the method or algorithm as the second. This is an optimization of the data, which is more convenient to operate and simplifies the process.
(2) Features:

  • 1 is a kind of thought more in line with our thinking habits (lazy thought)
  • 2 can simplify complex things
  • 3 changed us from executor to commander

2. Overview of classes and objects

(1) A collection of objects with the same characteristics and behavior is a class, and an object is an instance of a class.
For example: a person is a class, and a specific person is an object of the class.
(2) Defining a class is actually defining its members:
The member variable is the same as the previously defined variable, except that the position has changed. In a class, outside a method.
The member method is the same as the previously defined method, except that static is removed. The function of static will be explained in detail later.

3. Create and use classes

Use your own defined classes:
Class is an abstract concept and cannot be used directly.
If we want to use this class, we need to instantiate it.
The so-called instantiation is to create the object of this class, which is the concrete expression of this class.

How to create an object of a class, or how to instantiate a class?
Use the keyword new to operate.

3.1 creating a human

public class Person {
    String name = "Zhang San";
    int age = 18;
    char sex = 'male';

    //Define member methods
    public void eat() {
        System.out.println("having dinner");

    public void sleep() {

    public void playGame() {
        System.out.println("play a game");

public class MyTest {
    public static void main(String[] args) {
        //Create an object of the Person class
        Person p = new Person();
        //Use the objects of this class to call the functions and properties in the class
        String name =;
        int age = p.age;
        char sex =;

        System.out.println("full name:" + name);
        System.out.println("Age:" + age);
        System.out.println("Gender:" + sex);

        //Call functions in class;

        //You can create many objects of this class

        Person p2 = new Person();
        //Set the value of the member variable = "Wang Wu";
        p2.age = 30; = 'female';
        String name1 =;



Now a student class is given. The memory diagrams of one object, two objects and three objects are explained in detail below

class Student {
private String name;
private int age;

public Student(){}
public Student(String name, int age){ = name;
this.age = age;

public void setName(String 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 );

3.2 detailed explanation of an object memory diagram

class Test1 {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println( s.getName());
        System.out.println( s.getAge());

        System.out.println( s.getName());
        System.out.println( s.getAge());

3.3 detailed explanation of memory diagram of two objects

public class Test2 {
    public static void main(String[] args) {
        Student s1 = new Student("Xiao Li", 18);
        Student s2 = new Student("Xiao Ming", 20);;;

        s2.setName("Xiao Liang");;;

3.3 detailed explanation of memory diagram of three objects

public class Test3 {
    public static void main(String[] args) {
        Student s1 = new Student("Xiao Li", 18);
        Student s2 = new Student("Xiao Ming", 20);
        Student s3 = s1;

4. Differences between member variables and local variables

Member variablelocal variable
Outside method in classIn the method definition or on the method declaration
Memory heapIn stack memory
Exists with the creation of the object and disappears with the disappearance of the objectIt exists as the method is called and disappears as the method is called
There are default initialization valuesThere is no default initialization value. It must be defined and assigned before it can be used.

Note: local variable names can be the same as member variable names. When using methods, the principle of proximity is adopted. First find in the local, find and use it. The local includes local variables and formal parameters. If it is not found in the part, then find it in the class and use it when it is found.

5. Parameter transfer in Java

Formal parameters
Basic type: the change of formal parameters does not affect the actual parameters
Reference type: the change of formal parameters directly affects the actual parameters

If you see that the formal parameter of a method is a class type (reference type), what you need here is the object of this class.

6. Anonymous objects

Anonymous object: an object without a name.
Usage scenario:
When a method is called only once.
Anonymous objects can be passed as actual parameters.

7.private keyword

(1) Is a permission modifier
(2) You can modify member variables and member methods
(3) Members decorated by them can only be accessed in this class
Most common applications:
(1) Modify member variables with private
(2) Provide the corresponding getXxx() and setXxx() methods

8.this keyword

If the names of local variables and member variables are the same, the member variables will be hidden. At this time, you need to use this keyword to call the member variables.
this refers to the caller who calls the method or variable. It is a reference to an object. this refers to which object calls the method or variable.

Keywords: Java

Added by soupy127 on Sat, 19 Feb 2022 21:18:35 +0200