Analysis of Java class and object & & encapsulation & & construction method

Posted by nicandre on Sat, 30 Oct 2021 15:55:47 +0200

⭐ preface ⭐

This article introduces classes and objects in Java.

πŸŽ‰ Welcome to pay attention πŸ”Ž give the thumbs-up πŸ‘ Collection ⭐ Leave a message πŸ“
πŸ”‘GitHub Java warehouse, welcome to visitπŸ”‘
πŸ“The blogger's note link is updated at the first timeπŸ“

🌲 class

Classes are templates or blueprints for constructing objects. The process of constructing objects from classes is called creating instances of classes.

Members of a class can contain the following:

  1. field

    Attributes or members are divided into ordinary member variables and static member variables

    Common member variable: it belongs to an object, is placed in the heap, and is accessed through an object

    Static member variable: it belongs to a class and is placed in the method area, also known as class variable. It is accessed through class name

  2. method

  3. Code block

  4. Inner class

  5. Interface

Ordinary member

class Person{
    public String name;
    public int age;
    public void eat(){
        System.out.println(name+" is eat");
    }//method
}
public class Main {
    public static void main(String[] args) {
        Person play1=new Person();//Class is instantiated as an object
        System.out.println(play1.name);
        System.out.println(play1.age);
        play1.eat();
    }
}

We do not initialize when using. The default string of reference type is null, integer is 0, and Boolean is false

We initialize it before accessing it

public class Main {
    public static void main(String[] args) {
        Person play1=new Person();
        play1.name="number1";
        play1.age=18;
        Person play2=new Person();
        play2.name="number2";
        play2.age=19;
        System.out.println("name:"+play1.name+" Age:"+play1.age);
        System.out.println("name:"+play2.name+" Age:"+play2.age);
    }
}

Name: number1 age: 18
Name: number2 age: 19

From this, we can see that ordinary member variables belong to objects. When we instantiate the second object and initialize the assignment, it will not affect the value of the first object.

Static member variable

Ordinary member variables belong to objects, while static members belong to classes

class Person{
    public String name;
    public int age;
    public static int count;
}
public class Main {
    public static void main(String[] args) {
        Person play1=new Person();
        Person play2=new Person();
        play1.count=1;
        play2.count=2;
        System.out.println(play1.count);
        System.out.println(play2.count);
    }
}

We see

  1. There are warnings about accessing static variables through objects
  2. "Initialization" is different, but the output result is the same, and the result is the second

In fact, static member variables belong to classes. We can directly access static variables through classes without instantiating an object

We know that the reference is placed on the stack and the object is placed on the heap. Where are the static variables? Put it in the method area

This corresponds to static methods, which we can access directly through classes.

The variables defined by static belong to classes, which are called class variables; The methods defined are called class methods.

It should be noted that

  1. Static variables cannot be defined within a normal member method. (you can see direct error reporting, which is not supported by syntax.)

  2. Ordinary methods cannot be called inside static methods (objects are required to call ordinary methods)

  3. Static methods can be called inside ordinary methods

  4. Static methods can access static variables internally, but static variables cannot be defined

🌲 encapsulation

private implementation encapsulation

The two keywords private and public represent "access control".

  1. The member variable or member method modified by public can be called by the direct caller of the class.
  2. The member variable or member method modified by private cannot be called by the caller of the class.

In other words, the user of a class does not need to know or pay attention to the private members of a class, so that the class caller can use the class at a lower cost.

getter and setter methods

class Person{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class Main {
    public static void main(String[] args) {
        Person person =new Person();
        person.setAge(18);
        person.setName("number");
        System.out.println(person.getAge());
        System.out.println(person.getName());
    }
}

It should be noted that

The set and get methods are provided in the idea compiler and can be generated directly.

Note the following errors

public void setAge(int age) {
        age = age;//No assignment, two ages are the same age (local variables are limited)
}

🌲 Construction method

Definition: the method name and class name are the same, and the constructor has no return value.

Step: allocate memory for the object ----- > call the appropriate allocation method

Appropriate means that more than one method is called.

Note that

  1. The method name must be the same as the class name
  2. Constructor has no return value type declaration
  3. There must be at least one construction method in each class (if there is no explicit definition, the system will automatically generate a parameterless construction)
  4. If no constructor is provided in the class, the compiler generates a constructor without parameters by default
  5. If a constructor is defined in a class, the default parameterless constructor will no longer be generated
  6. The constructor supports overloading. The rules are consistent with the overloading of ordinary methods
class Person{
    public String name;
    public int age;

    public Person(String name,int age){
        this.age=age;
        this.name=name;
    }
    public Person(String name){
        this.name=name;
    }
    public Person(){
        System.out.println("No parameters");
    }
}
public class Main {
    public static void main(String[] args) {
        Person person1 =new Person();
        Person person2 =new Person("zhangsan");
        Person person3 =new Person("lisi",18);
        System.out.println(person1.name+"   "+person1.age);
        System.out.println(person2.name+"   "+person2.age);
        System.out.println(person3.name+"   "+person3.age);

    }
}

No parameters
null 0
zhangsan 0
lisi 18

🌲 Keyword this

What's the difference between this and super?

this.data -------- call the properties of the current object

this.func() ----- call the method of the current object

this() ---------------- call other construction methods of the current object, which can only be stored in the constructor

Must be placed on the first line

🌲 Code block

Local code block

Instance code block

Static code block

Synchronous code block

class Person{
    public String name;
    public int age;
    {
        System.out.println("Instance code block");
    }
    static {
        System.out.println("Static code block");
    }
    static int a=0;
}
public class Main {
    public static void main(String[] args) {
        Person person=new Person();
        System.out.println(person);
    }
}

Static code block
Instance code block
Person@1b6d3586

We can see that static code blocks are executed first, and static code blocks are executed only once

And static code blocks are executed without instantiating objects

How are code blocks called?

The static code block is executed before the instance code block, and the static code block is executed only once

It is executed once even if it is not instantiated as an object

πŸ’Ž complimentary close πŸ’Ž

The blog is changed according to the notes. If there are mistakes, please comment and correct them. Thank you.

Topics: Java Back-end JavaSE