Summary of class and object knowledge points in java

Keywords: Java Back-end

1. Object oriented

(1) Object oriented understanding

  • Object-oriented is based on object, which mainly emphasizes object.

  • Face object is an idea to solve problems, which mainly depends on the interaction between objects to complete a thing

(2) Object oriented features

  • Object oriented is a common idea, which is more in line with people's thinking habits;
  • Object oriented can simplify complex business logic and enhance code reusability;
  • Object oriented has the characteristics of abstraction, encapsulation, inheritance, polymorphism and so on.

  2. Classes and objects

(1) Class is the template of an object. It mainly describes the attributes and functions of an object.

(2) Characteristics of class: class is the type of object and a collection of objects with the same attributes and methods.

(3) Attributes: characteristics of objects (each object's attribute has its own value)

(4) Method: the operation (function) that the object can perform

(5) Relationship between class and object: class is the abstract template of object; An object is a concrete entity

3. Definition class

class +classname{

Attributes (characteristics)

Method (function)


Class is the keyword defining the class, classname is the class name, and {} is the body of the class.

The content contained in a class is called a member of the class. Attribute is mainly used to describe a class, which is called class member attribute or class member variable. Method mainly describes the functions of the class, which is called the member method of the class.

public class Student{

String name;
int height;//attribute

public void  dohomework(){



4. Construction method

Constructor (also known as constructor) is a special member method. The name must be the same as the class name. When creating an object, it is called automatically by the compiler and only once in the whole object life cycle.

public class Student{

String name;
int height;

public Student(String name,int height){;


// Construction method:
  // The name is the same as the class name, there is no return value type, and it is not allowed to set it to void
  // In general, the public modifier is used
  // Called automatically by the compiler when an object is created, and only once in the object's life cycle

public Student(){"Xiao Ming";


//Construction methods can be overloaded (users can provide construction methods with different parameters according to their own needs)
//The above two construction methods have the same name and different parameter lists, so they constitute method overloading.
// If the user does not explicitly define it, the compiler will generate a default constructor. The generated default constructor must be parameterless.

be careful:

The constructor is only responsible for initializing the members in the object and is not responsible for opening up space for them

5. Class instantiation

Defining a class is like defining a new type in the computer, just like int, long, double and other types (except they are built-in in java). How do we use the class after we define it?

Just like defining variables, we can define objects (new objects).

public class student{
String name;
int height;

public class UserStudent {
    public static void main(String[] args) {
        Student s=new Student();"Zhang San";
        Student b=new Student();"Li Si";


Here, through the new keyword and class name, we instantiate two objects: s and b

new + class name (parameter list of constructor to be called)

Student s=new Student()

We can access the properties of these two objects through

be careful:
The new keyword is used to create an instance of an object
Use. To access properties and methods in an object
Multiple instances of the same class can be created

6.this reference

(1) What is this reference

The java compiler adds a hidden reference type parameter to each "member method", which points to the current object (the object that calls the member method when the member method runs) , all member variable operations in the member method are accessed through this reference, but all operations are transparent to the user, that is, the user does not need to pass them, and the compiler completes them automatically.

public class Student{

private String name;
private int height;

public Student(String name,int height){;

public void printStudent(){



(2) Properties of this reference

1. Type of this: corresponding class type reference, that is, which object is called is the reference type of which object
2. this can only be used in member method
3. In "member method", this can only refer to the current object, and can no longer refer to other objects. It has final attribute
4. this is the first hidden parameter of the "member method", which will be automatically passed by the compiler. When the member method is executed, the compiler will be responsible for passing the reference of the calling member method object to the member method, and this is responsible for receiving it

Posted by rkeppert on Tue, 30 Nov 2021 02:39:32 -0800