[javase basics] Chapter 9 Classes and objects focus

👀 About the author: Hello everyone, my name is Dasugi.
🚩🚩 Personal homepage: Dashan who loves programming
Support me: Like + Follow~ Don’t get lost🧡🧡🧡 ✔Series
column: [javase] basics⚡⚡⚡
(❁´◡`❁) Inspirational motto: Life is too short for me , cannot be used to hold grudges. In this world, everyone must be at fault. (from “Jane Eyre”)

Article directory

The relationship between [classes and objects]

A class is a collection of abstract features, and an object is an instantiated embodiment of the features provided by the class .

2. Class encapsulation

1. The meaning of encapsulation

1.🔥When we write code, we often involve two roles:Class implementers and class callers2.
The essence of encapsulation is that the caller of the class does not need to know too much about how the implementer of the class implements the class, as long as he knows how to use the class.
3.⛳📣 In short, the custom class is encapsulated Certain member variables and methods in the class are protected from direct access by callers of the class. Improved security and reduced code maintenance costs

2. private implementation encapsulation

1.private modified member variable

private String name; //Encapsulate member variable 
 private  static  double height;
  private  static  double weight;

  1. private modification method
  1. Similar to the above, use private before the return type of the method.
  2. Note:A privately decorated method can only be called by a method in the current class in the current class. But a method in one class cannot call a private method in another class.
  3. The private modified method cannot be called in the main function, but can only be called by the method in the current class in the current class

Calling in the main method will throw an error

3. The getter and setter methods create a public interface

Although the method modified by private cannot be called by main, the member variable modified by private can be called indirectly through the getter and setter methods

public String name;
        return name;

    public void setName(String name) {

        this.name = name;

3. Static modified member variables and methods

1.static modifier member variable

important point
🏀.static modified member variables or methods are directly dependent on the class and not dependent on the object; so the main function can directly call the member through the class name

2. static modified method

🏀. A static method modified by static cannot call a method not modified by static, but vice versa

🏀.static Modified members only have one copy.

How to understand that there is only one copy? please look below! 🤞🤞

4. Analysis of the storage location of each member in the custom class

but the variables of members modified by static will be placed in the method area, and new variables will not be copied with the creation of the object. so only one

Note: 1 The method area is not necessarily a method!
2. There is no way to say how to allocate memory

Five.this keyword

1 Why is there this?
Let’s look at an example

public void setMyName(String MyName){

After adding this, it means the outside MyName variable
because this represents a reference to the current variable
2. In addition to this, this has other uses in the constructor

6. Precautions for construction methods

1. The purpose of the constructor

Assign value to member variable

2. Precautions

  1. Precautions

When no constructor is added manually, the system defaults to a no-argument constructor. After manually adding a constructor, there will not be a built-in no-argument constructor by default.

3. Frequently asked interview questions

Q: The this keyword represents an object. Is this sentence correct or not?
To answer this question, we must first clarify, how many steps are there to generate the object?
1. Allocate memory for the object.
2. Invoke the appropriate constructor.
Answer: Obviously different, because the this keyword is actually used in the process of calling the constructor, and the object will not be created until the constructor is called. (As mentioned above, this represents a reference to the current object)

7. Code block

1. Instance code block (defined in the class)

2. Static code blocks

Note: Non-static variables cannot be used in static code blocks. You can’t use this because this represents a reference to the current object and is based on the object. The static code block is modified by static and is class-based

Leave a Comment

Your email address will not be published. Required fields are marked *