The difference between static methods and instance methods is mainly reflected in two aspects:
When calling static methods externally, you can use the "class name. method name" method or the "object name. method name". The example method is only the latter method. That is, calling a static method can eliminate the need to create an object.
When accessing members of this class, static methods only allow access to static members (i.e., static member variables and static methods), but do not allow access to instance member variables and instance methods; instance methods have no such restrictions.
The following examples show this difference.
1. Example of calling static method
//------------ hasStaticMethod.java---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The following program uses two forms to call static methods.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The program calls static methods twice, both allowable, and the output of the program is as follows:
This is a static method.This is a static method.
Allowing static methods to be called without creating objects is that in order to reduce the hassle of programmers when calling certain common methods, Java allows programmers to use methods in the traditional C language using functions. A typical example is that some previous programs use "Math.ramdon()" to get random numbers.
Another typical representative is Arrays, the array processing tool
2. Example of static method access member variables
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //Correct, you can have your own local variable sa = 15; //Correct, you can use the static variable ia = 30; //Correct, you can use the instance variable statMethod(); //Correct, you can call the static method}}
This example can actually be summarized in one sentence: a static method can only access static members, and an instance method can access static and instance members. The reason why static methods are not allowed to access instance member variables is because instance member variables belong to an object, and when static methods are executed, objects do not necessarily exist. Similarly, because an instance method can access instance member variables, if a static method is allowed to call an instance method, it will indirectly allow it to use the instance member variable, so it cannot call the instance method either. Based on the same principle, the keyword this cannot be used in static methods.
The main() method is a typical static method, which also follows the rules of general static methods, so it can be called by the system before creating the object.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.