The difference between a Java static method and an instance method

Source: Internet
Author: User
Tags instance method

The difference between static and instance methods is mainly in two ways: 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 OA = new Hasstaticmethod (); Create an Object
Oa.callme (); Using objects to invoke static methods
}
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; Defining an instance member variable
The following defines a static method
static void Statmethod () {

Correct, static methods can use static variables

Correct, you can call a static method
ia = 20; Error, cannot use instance variable
Insmethod (); Error, cannot invoke instance method
}


void Insmethod () {
int i = 0; Correct, can have their own local variables
SA = 15; Correct, you can use static variables
IA = 30; Correct, you can use instance variables
Statmethod (); Correct, you can call a 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 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.