Java Basics - memory pointing 1

Posted by pvechi on Sat, 12 Feb 2022 03:47:41 +0100

What is an object?

public class Cat {
	public int age;
	public String name;
	public char sex;
	public void run(int ag) {
		this.age=ag;
	}
	public static void main(String[] args) {
		Cat cat1=new Cat();
		Cat cat2=new Cat();
		Cat cat3=new Cat();
		
		cat1.run(10);
		cat2.run(11);
		System.out.println(cat1.age);
		System.out.println(cat2.age);
	}
}

We call the run() method of cat1 and the run() method of cat2, execute the run() method, and then operate our variables to make value changes. Finally, we output the age value of each cat1 and cat2, and we will find that the output is inconsistent, which means that we have at least two age variables in memory, belonging to cat1 and cat2 respectively, Then we can also deduce that there is an age variable in cat3.

There is at least one memory space that can be managed by cat1 and cat3.

From the above figure, we can know that this memory is our object

What is a class, what is an object, and what is the relationship between a class and an object?

The class has only the definition of information, and the object has the value of this information

A class is the template of an object. We can imagine a class as a cookie cutter and an object as a cookie. The process of constructing an object from a class is called class instantiation.  

this keyword

In order to solve the naming conflict and uncertainty of variables in Java, the keyword this is introduced to represent the reference of the current object of its method. This represents the current object

this. Attribute name: the names of local variables and member variables in a method are the same. Our program needs to access member variables in this method. At this time, we must use this keyword to distinguish between member variables and parameters in the method. For example, we use the set method

private String name;
public void setName(String name) {
		this.name = name;
        //The name attribute of the current object
}

this. Method name: let a method in the class access another method or instance variable in the class

    public void souPerson() {
		System.out.println("I am a person....");
	}
	
	public void name() {
		this.souPerson();
	}
	
	public static void main(String[] args) {
		Person person = new Person();
		person.name();
        //You cannot use this name
	}

[note] static keyword refers to the class, and this keyword refers to the current object. Therefore, this cannot be used in the method modified by staic.

this(): can be used to access the constructor of this class

	public Person(String name,int  age) {
		this.age  = age;
		this.name = name;
	}
 
	public Person() {
		this("Zhang San",10);
	}

[note]

1.this() cannot be used in ordinary methods, but only in construction methods

2.this() must be the first statement used in the constructor

3. Two constructor methods under a class cannot call each other through this()

4. Cannot be used with super()

static keyword - class specific

Static makes it easy to call (method / variable) without creating an object. Obviously, the method or variable modified by the static keyword does not need to rely on the object for access. As long as the class is loaded, it can be accessed through the class name.

 public class Person {
	static String from;
    public static void Run() {
		System.out.println("run");
	}
	public static void main(String[] args) {
		Person.from = "China";
        Person.Run();
	}
}

static memory usage

Static keyword is stored in the static constant pool in our method area. Static modified methods, variables and code blocks can be shared

public class Person {
	private int age ;
	private String name;
	static String from;
	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + ", from=" + from + "]";
	}
	public static void main(String[] args) {
		Person person1 = new Person(20,"Zhang San");
		Person person2 = new Person(21,"Li Si");
		Person.from = "China";
		System.out.println(person1.toString());
		System.out.println(person2.toString());
	}
}
/*
Person [age=20, name=Zhang San, from = China]
Person [age=21, name=Li Si, from = China]
*/

The values of the name and age attributes of person1 and person2 are stored in the heap memory and are private to the object, but the value of the from attribute is stored in the static constant pool of the method area and belongs to the public.

static modifier

Topics: Java Back-end