The difference between static method and instance method is mainly embodied in two aspects:
When you call a static method externally, you can use the "class name. Method Name" way, or you can use the "object name. Method Name" way. The instance method is only in the following way. That is, calling a static method eliminates the need to create an object.
When a static method accesses members of this class, it only allows access to static members (that is, static member variables and static methods), not to instance member variables and instance methods, and the instance method does not.
The following examples show the difference.
1. Call the static method example.
-----------Hasstaticmethod.java-----------------public class hasstaticmethod{//defines a static method public static void CallMe ( {System.out.println ("This is a static method.");}}
The program two calls to static methods are allowed, the output of the program is as follows:
Allowing a static method to be called without creating an object is Java's trouble in reducing the programmer's call to some common methods, allowing programmers to use the method in the traditional C language using functions. A typical example is the use of "Math.ramdon ()" In some of the previous programs to obtain a random number.
There is also a typical representation of the array processing tool arrays
2. The static method accesses the member variable example.
-----------Accessmember.java-----------------class accessmember{private static int sa;//define a static member variable private int ia; Define an instance member variable//The following defines a static method that is statically void Statmethod () {int i = 0; Correct, can have its own local variable sa = ten; Correct, static method can use static variable Otherstat (); Correct, you can call the static method ia =; Error, cannot use instance variable Insmethod (); Error, cannot invoke instance method}static void Otherstat () {}///below defines an instance method void Insmethod () {int i = 0; Correctly, you can have your own local variable SA = n; Correct, you can use the static variable ia =; Correct, you can use the instance variable Statmethod ();
In fact, this example can be summed up in one sentence: static methods can only access static members, and instance methods have access to static and instance members. Static methods are not allowed to access instance member variables because instance member variables are part of an object, and static methods do not necessarily have objects when they are executed. Similarly, because an instance method can access an instance member variable, allowing a static method to invoke an instance method indirectly allows it to use an instance member variable, so it cannot invoke an instance method. For the same reason, the keyword this is not used in static methods.
The main () method is a typical static method that also follows the rules of a general static method, so it can be called by the system before the object is created.
The difference between a Java static method and an instance method