The difference between Java static methods and instance methods

Source: Internet
Author: User
Tags define instance method

The difference between static method and instance method is mainly embodied in two aspects:

You can use the class name when calling a static method externally. Method name, you can also use the method of "object name. Method Name." And the instance method is only the latter way. That is, calling a static method eliminates the need to create an object.

When a static method accesses a member of this class, it only allows access to static members (that is, static member variables and static methods), without access to instance member variables and instance methods, and the instance method does not.

The following examples illustrate this distinction.

1, call the static method example.

-----------Hasstaticmethod.java-----------------Public  
class hasstaticmethod{  
//define a static method  
public static void CallMe () {  
System.out.println ("This was a static method.");  
}  
       
The following program uses two forms to invoke the static method.  
//-----------Invokestaticmethod.java-----------------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  
}  
}

The program two times calls the static method, is allowed, the program output is as follows:

This is a static method. This is a static method.

Allowing a static method to be invoked without creating an object is Java, which allows programmers to use the method in a traditional C language by using a function in a way that reduces the hassle of programmers calling some common methods. A typical example is the use of "Math.ramdon ()" In some previous programs to get random numbers.

There is also a typical representative is the array of processing tools arrays

2, static method access member variable sample.

-----------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  
, static void Statmethod () {  
int i = 0;    Correctly, you can have your 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 =;    Correct, you can use the static variable  
ia =;    Correct, you can use the instance variable  
statmethod ();   Correct, you can call the static method  
}  
}

This example can be summed up in one sentence: static methods can access only 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 belong to an object, and static methods do not necessarily exist when they are executed. Similarly, because an instance method can access an instance member variable, if a static method is allowed to call an instance method, it is indirectly allowed to use the instance member variable, so it cannot invoke the instance method. For the same reason, the keyword this is not available in static methods.

The main () method is a typical static method that follows the rules of a general static method, so it can be invoked 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.