Java stack static

Source: Internet
Author: User

So the difference between a static variable and a non-static variable is that a static variable can be used to count, not a static variable.

Understand the memory, understand everything, understand a variety of languages. All the languages are like this: The local variable allocation memory is always inside the stack, new out of the allocated memory is always in the heap, static things allocated memory is always in the data area. The rest of the code must be in the code area. This is true of all languages.

In a static method, if you want to access a non-static member variable, it is not directly accessible, it must be in a static method of the new object to be accessed. If the static member variable is added, then this member variable is a static member variable and can be accessed directly within the main method.

The main method is a static method, and the main method does not need to be a new object when it is executed.

A dynamic method is called on an object, and a static method is not invoked on an object, and no object can be used. You can use the form of "Classname.method ()" To invoke a static method. Therefore, it is not possible to access non-static member variables in the main method, it is not possible to access the non-static method inside the main method, because the non-static method can only be called for an object, there is no object, can not find the executor of the method.

Member variables allocate storage space in the heap memory only when new objects come out. Local variables allocate storage space inside the stack memory.

Static methods are no longer called on an object, so non-static members cannot be accessed.

A non-static member is dedicated to an object, and a non-static member must have a new object in order to access it.

Static variables can be accessed through the object name or by the class name, both of which are accessed by the same piece of memory.

Java stack static

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.