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.
-----------file name Hasstaticmethod.java, program number 1-----------------public class hasstaticmethod{//defines a static method public static void CallMe () {System.out.println ("This is a static method.");}
The following program uses two forms to invoke a static method.
-----------file name invokestaticmethod.java,2-----------------public class Invokestaticmethod{public static void Main ( String args[]) {Hasstaticmethod.callme ();//Do not create object, call static method directly Hasstaticmethod OA = new Hasstaticmethod (); Create an object Oa.callme (); Use object to invoke static method}}
Program 3.36 two calls to the static method, are allowed, the output of the program is as follows:
This is a static method. This is a static method.
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.
2. The static method accesses the member variable example.
-----------file name Accessmember.java, program number 3.37-----------------class accessmember{private static int sa; Defines a static member variable private int ia; Define an instance member variable///define a static method for the following statically void Statmethod () {int i = 0;//correct, can have its own local variable SA = 10;//correct, static method can use static variable Otherstat ();//correct, A static method can be called IA = 20; Error, cannot use instance variable Insmethod (); Error, cannot invoke instance method}static void Otherstat () {}///Defines an instance method below void Insmethod () {int i = 0;//correct, can have its own local variable SA = 15;//correct, can use static variable IA = 30; Correct, you can use the instance variable Statmethod (); Correct, you can call the static method}}
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 static method and an instance method