The difference between a Java static method and an instance method

Source: Internet
Author: User

Http://www.cnblogs.com/shenliang123/archive/2011/10/27/2226923.html Original Address

The difference between a Java static method and an instance method
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{//define 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.
-----------Invokestaticmethod.java-----------------public class invokestaticmethod{public static void main (String  Args[]) {Hasstaticmethod.callme (); Do not create objects, call static methods directly  
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;  Correctly, you can have your own local variable sa = 10;    //Correct, static method can use static variable Otherstat ();  correctly, the static method can be called IA = 20;  Error, cannot use instance variable Insmethod (); Error, cannot invoke instance method} static void Otherstat () {}  //The following defines an instance 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 Java static method and an instance method

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.