Java-Preliminary Understanding-Seventh Chapter-this keyword memory plot

Source: Internet
Author: User

One.

For static use and precautions have been explained, and then an example, the static memory area involved in the form of a diagram to show you.

here is a description of the person, there are two member variable name,age, and there is a static variable country. The next person gives us a constructor that can initialize both performance and age. Next there is a non-static method show that can show the personal information of this person. Then there is a static method that can show us the nationality.

In the main function we call the static method in the form of the class name, and then the object of the class is created, and the object of the class calls a non-static method, which can only be called statically.

In the above example, the statement inside the main function is p=new person (); This shows that there are no parameters in parentheses. In the description class, there is no constructor for null arguments, and constructor initialization is not possible here.

Take the above description as an example to show the in-memory process. (I think I'm still trying to string up all the object-oriented knowledge that I've learned so far, to sort out the sequencing.) )

The above content is very unusual, did not compile the StaticDemo2 source program, but directly with the Java command to run the StsticDemo2 class. That is, it runs without compiling?

This is running a class that will be loaded into memory, and the process of this class being encapsulated object is not now. This is the part to be talked about later. The space is opened up as soon as the load comes in.

In any case, computer memory has long been divided into several parts to store different things separately. There are currently three memory areas involved, stack, heap, method area.

Where are the data after the zoning is complete? is a problem.

Where does the method exist? Methods are stored in the method area. Parse how the method is stored in the method area.

When the StaticDemo2 class is executed, the class is actually in memory (the program runs in memory). When this class is in memory, it opens up space.

In the method area divides a space, the storage StaticDemo2 class, after dividing, this class inside has a member method, is StaticDemo2 (), This method does not know where to come from ? This method is the default constructor in this class . are loaded along with the class.

The main function is static, and main () is loaded into the static zone, which is dedicated to storing the static data. The static area is also marked as a StaticDemo2 class, and then there is a method for static main ().

The top left and right, the left is the non-static area, the right side is the static area. both non-static and Static-zone methods are shared . Why? In terms of the show () method in the Description class, all objects have this function, and the only difference is what? Is that the data that the object encapsulates is not the same. Some objects called Zhang San, some objects on the John Doe, but everyone will speak, will say their name and age, so this function is shared, this function is in the method area. Only the method distinguishes between static and non-static, because the calling method is different from the owning one. All members of a non-static zone have a this belongs to. Why? Non-static content can only be called by an object. and to the static zone, it has the class name to which it belongs.

On the console, the Java StaticDemo2 back to the car and it's loaded. Once loaded, the virtual machine uses the class name directly and invokes the method. At this point, the main method is in the stack. After the main method is in the stack, it will run inside the content, but the code inside the main method is stored in the static zone. Because everything in the source program has been converted into bytecode files stored up. The Mian itself is in the static zone, and its code is also in the static area.

The first sentence person.show () uses a class, at which point the class is loaded. This is the time to find out if there are person.class files under the classpath path. If the classpath is not set, it will be found by default in the current directory, and will be loaded into memory after the Person.class file is found. A load will be allocated in the method area. The person class is in memory, followed by the person's constructor and the Show method. When they are loaded, the method of the static method is also loaded, and the method belongs to the person class. This is what we call static zones, all of which are shared data. And then there's no end to it, and there's country,country. Default is initialized to null and then initialized to "CN",

After loading, it is Person.method (), which is the class name in the call. Called with the class name, only the static zone can be found. To the static area to find, in the person this class belongs to the static zone or in the static area of the person class, the person class area, there is no Main method? If you have one, the method will go into the stack. Does this method hold this? No, only the non-static zone has this. So far, there is nothing in the heap. Note that method methods are from the static zone in the method area to the stack memory. The right dotted line below is the code store, and some are called method tables. The method in the stack is the run area, which will have local variables and run out of release. Local variables for storing methods in the stack area, System.out.println is not in,

When the method is finished, it starts running and executes the method action code as it runs. There are variables to open up space, no variable space after the opening will be released. Once run, the person.country is output, and it calls country a variable. It is found that this variable is the class name. If you do not write the class name, it is also in the class file. Call the time, is to find the person in the static variable, a find, the CN to the output. After the output, and then down is the Return;method method on the bomb stack, to this, the method area is loaded complete.

Then execute the second sentence, person p=new person ("Java", 20); And then the main function has more than one p,= to the right is a person. In heap memory, space is allocated, the address number is assigned, the member variable is loaded, and the default initialization value. Of course, in the class description, it can describe its own name and age, but the existence of the type is used, must be stored in the object, because the name and age value can only exist in the object. After the default initialization is complete, the constructor is initialized. At this point, it is necessary to look back, the corresponding constructor function. At this point, the constructor is in the stack, and the constructor accesses the data in the object, which initializes the data in the object. Which object to initialize? It does not know, so it holds a this, and this is exactly the 0x0056 object in the call constructor, so this is equal to 0x0056. The name that follows it is assigned the value "Java", and Age is assigned "20". Immediately after this function is finished, start execution, the constructor inside the code, This.name=name, This.age=age, consistency, found the constructor inside the name assignment to THIS.name, and this is the 0x0056 object, that is, name = "Java" assigns a value to the name in the object.

At this point, the constructor is finished, and it is going to play the stack. Immediately after that, the initialization of the object in the right heap is over. The address number 0x0056 is then assigned to the left P, so that p points to the object.

Finally, execute p.show () and call the Show method. There is a show method in the non-static area of the method area. Note: When the p.show is compiled, it checks whether the source file has a show, and if not, the compilation fails. But when running, it will also check, found show, let show into the stack. Show is non-static, show must have this belongs to 0x0056, execute inside code, found no local variable, only output statement. Executes the output statement directly. In the output, the discovery output country, we all know in counrty after compiling, in the generation class file, there is already a class name belongs to, so it is to find the contents of the static area. After the search, the CN is printed. Then find the value of name and age, with this in front of name and age, and print the value of 0x0056, which points to the name and age values in the object. When it is found, it is output, cn,java,20. When the output is finished, the Show method pops up.

After its stack, the main function of the last word is return, which is the default, implicit, write can not write, the system will automatically help you add, function to end. The constructor has a return statement, and the general method has a return statement. Return can only be omitted if void is the case. The main function has Renturn, also the stack. Then there was no code, and the virtual machine ended. This involves threading.

Java-Preliminary Understanding-Seventh Chapter-this keyword memory plot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.