Differences between static and instance members in Java and main functions

Source: Internet
Author: User

From: http://www.cnblogs.com/shenliang123/archive/2011/10/27/2226923.html

When calling static methods externally, you can use the "class name. Method Name" method, or the "Object Name. Method Name" method. The instance method is only followed by this method. That is to say, you can call static methods without creating objects. Specifically, the main method of a class is limited by public static void. Therefore, this method can be called directly by class name in other classes, for example, you can directly call the main method of A without creating an object of A in Class B:. main (PARAM)

Static Method accessMembers of this class (you must note that it is a member of this class. when accessing members of an external class, this restriction is completely absent !!!In fact, this is also true. If there are restrictions when accessing external class members, the main function will not be able to execute some non-static methods of the external class, it is useless when it comes to inheritance .) You can only access static members (static member variables and static methods), but not instance member variables and instance methods.The main () method is a typical static method.It also follows General static method rules, so it can be called by the system before the object is created.

For specific verification examples can refer to: http://www.cnblogs.com/shenliang123/archive/2011/10/27/2226923.html

This example can be summarized as one sentence: (Note the premise: when accessing members of this class) static methods can only access static members, and instance methods can access static and instance members. The reason why static methods are not allowed to access instance member variables is that instance member variables belong to an object, while static methods do not necessarily exist during execution (static members do not belong to an object, it is shared by all the members of this class, which is similar to the role of the static data type in the class in C ++ ). Similarly, because the instance method can access the instance member variable, if the static method is allowed to call the instance method, it will indirectly allow it to use the instance member variable, so it cannot call the instance method. In the same way, the keyword "this" cannot be used in static methods.

Main function: each Java file that writes the class contains a main method that can be used to run (if you need to run the main method). As described above, all classes must exist in one package, and methods must exist in one class (the main method is also a special method ). Each package can contain N java files. Therefore, each package can contain several main methods (functions ).

 

Related Article

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.