Why can't Java static methods be accessed?

Source: Internet
Author: User

Why can't Java static methods be accessed?

Non-static methods (without static) can access static methods (with static), but in turn won't work. Why?

Public class test {public void static main (String args []) {method (); // an error occurs. You are prompted to change the method to the static method2 (); // The call method is correct. new Test2 (). method (); // correct} public void method () {System. out. println ("HelloWorld");} public static void method2 () {System. out. println ("HelloWorld") ;}} public class Test2 {public void method () {System. out. println ("HelloWorld ");}}

This should be analyzed from the memory mechanism of java. First, when you create an object, it does not first open up memory space for the object in the heap, instead, load the code of the static method (static function with static modification) in the class to a place called the method area, and then create an object in the heap memory. Therefore, static methods will be loaded with the loading of classes. When you create an object, the object exists in the memory. The this keyword generally refers to this object. However, if there is no new object, you can call the static method of this class through the class name.

The program is eventually executed in the memory. variables are accessed only when they have a place in the memory. The static members of the class (abnormal and method) belong to the class itself, when a class is loaded, the memory is allocated and can be accessed directly using the class name. Non-static members (variables and methods) are class objects, therefore, the memory is allocated only when the class Object Zen master (creates an instance), and then accessed through the class object.

An error occurs when a static member of a class accesses a non-static member because the static member already exists when the non-static member of the class does not exist, an error occurs when accessing something that does not exist in the memory.

When is the class loaded? It is loaded when it needs to be called.


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.