Java Execution Program Memory Analysis series column two static variables and methods memory analysis

Source: Internet
Author: User

Yesterday I wrote a simple chat about the simple memory partitioning of the Java Execution program, and today we'll talk about memory analysis of static variables and methods.

The first feature of 1.static variables and methods memory analysis


The first attribute of the STATIIC variable and method is called by the class name, the static variable, and the class name. The static method is invoked in the form of a normal object only. But specifically why is this, this has to analyze the static variables and methods how they are allocated in memory, so here I first set up a student class, there is a common member variable and a static member variable and a common method and a member method, and then set up a test student's class.

1    Public classStudent {2String name;//Normal member variables3      StaticString SS;//Static member variables4     Static voidSS () {5System.out.println ("I am a static method, can be called directly by the class name");6     }7      voidspeak () {8System.out.println ("I am a normal method, can only be called by object");9     }Ten } One  Public classStudenttest { A  -      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub theStudent.ss= "SDF"; - STUDENT.SS (); -         //Student.name Ordinary member variables cannot be accessed directly through the class name -         //Student.speak () Ordinary member method cannot be called by class name +Student s =NewStudent (); -S.ss= "SDF"; + S.SS (); A s.speak (); atS.name= "Lisong"; -          -     } -  -}

Here first load the Studenttest class to the method area, and then on the stack to allocate space for the variable, the specific execution process memory allocation has been analyzed in the previous http://www.cnblogs.com/songadai/p/7098649.html, So here is an unknown solution, the following is directly attached to the memory map. There should be a clear understanding of the static.

Careful observation, here I briefly explain why static variables and methods can be directly accessed through the class name, and ordinary member variables are not directly accessible, static zone is a special area, special in access to the class name directly to the static variables and methods of the area, and the code area and the constant area is not directly accessible.

The second characteristic of 2.static variables and methods memory analysis

The second attribute of the static variables and methods is that static methods cannot directly invoke normal member variable methods and ordinary member variables, but ordinary variables can call static methods and variables directly, and objects can also invoke static members and methods. In fact, when the object accesses the variable when the process of such, when the object is a access to a variable, first in the object's own heap space to find, if not found, then go to the static area to find the variable, if not found, the error.

A bit of note here is that when you create an object from a class, the static variable is not created, it belongs to the class only.

3. Conclusion

Memory Analysis Series The second one is almost over, and next we'll discuss some of the internal mechanisms for this,super.

Java Execution Program Memory Analysis series column two static variables and methods memory analysis

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.