Storage of static and non-static methods in < >java

Source: Internet
Author: User

Does non-static methods in Java share the same block of memory?

When a class is created with a instance, all of the instance field in this class will be added, will all instance method also add a copy? The answer is no, we use field to represent fields, methods are represented by method, then there are four kinds of static difference:

Class field: Useful field with static modifiers
Class method: A method that is useful for static modification
Instance field: Field not decorated with static
Instance method: No Static-Modified method

Then they are represented in memory as:

Class field: Share a piece of memory
Class method: Sharing a piece of memory
Instance field: With each instance a piece of memory
Instance method: Sharing a piece of memory

If the instance method also increases with the instance increase, then the memory consumption is too large, in order to achieve a common memory, Java is based on the This keyword, such as: Instance1.instancemethod (); Instance2.instancemethod (); When passed to an object parameter, the Java compiler automatically adds a This parameter, which means that the object reference is passed, although the two objects share a method, but the data produced in their method is private, because the parameter is passed in to the entry in the call stack. , and each object has a different call stack, so it's not confusing. In fact, when each non-static method is called, the Java compiler will automatically add the current call to this method object's parameters, and sometimes in one method call another method, you can not precede the this, because the object parameter to be passed is the object that is currently executing this method.

Why can't I call a non-static method in a static method? This is because the static method is directly related to class, when calling this method is called directly by the class, not the object, so the Java compiler has no object parameters to pass, so if you call a non-static method inside a static method, So how does the Java compiler determine which object is called by this non-static method? Yes, so the Java compiler will error, but it is not absolute, the Java compiler is implicitly passed the object parameters, then we can always display the parameters of the passed object, if we pass an object's reference to the static method, This reference can then be used to invoke non-static methods and access non-static data members.

Storage of static and non-static methods in < >java

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.