Differences between Java static methods and instance methods

Source: Internet
Author: User
The differences between static methods and instance methods are 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" method. The instance method is only followed by this method. That is to say, you can call static methods without creating objects.
 
When a static method accesses a member of this class, it only allows access to static members (static member variables and static methods), but does not allow access to instance member variables and instance methods; the instance method does not have this restriction.
 
The following examples show the difference.
 
1. Call a 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 call static methods.
// ----------- Invokestaticmethod. Java -----------------
Public class invokestaticmethod {
Public static void main (string ARGs []) {
Hasstaticmethod. callme (); // do not create an object, directly call the static method
Hasstaticmethod OA = new hasstaticmethod (); // create an object
Oa. callme (); // call static methods using objects
}
}
Both static methods are allowed by the program. The program output is as follows:
This is a static method.This is a static method. 
 
Allow calling static methods without creating objects. Java allows programmers to use functions in the traditional C language to reduce the trouble of calling some common methods. A typical example is that "math. Ramdon ()" is used in some previous programs to obtain random numbers.
Another typical example is the array processing tool arrays.
2. Static Method access member variable example.
 
// ----------- Accessmember. Java -----------------
Class accessmember {
Private Static int SA; // defines a static member variable.
Private int IA; // defines an instance member variable.
// A static method is defined below
Static void statmethod (){
Int I = 0; // correct. You can have your own local variable SA = 10;
// Correct. Static variables can be used in static methods.
Otherstat ();
// Correct. You can call the static method.
IA = 20; // error. instance variables cannot be used.
Insmethod (); // error. The instance method cannot be called.
}
Static void otherstat (){}
// Define an instance method below
Void insmethod (){
Int I = 0; // correct. You can have your own local variables.
Sa = 15; // correct. You can use static variables.
IA = 30; // correct. You can use instance variables.
Statmethod (); // correct. You can call static methods.
}
}
 
In this example, the static method can only access static members, and the instance method 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 a certain object, but static methods do not necessarily exist during execution. 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.
 
The main () method is a typical static method. It also follows the rules of the General static method, so it can be called by the system before the object is created.

 

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.