Java keyword--static

Source: Internet
Author: User

In Java, the keyword static is divided into three parts for discussion, namely Java static variable, Java static method, Java static class

Java Static Variables
    • Java instance variables is given separate memory for storage. If there is a need for a variable to being common to all the objects of a single Java class and then the static modifier should be used in the variable declaration.
    • Any Java object, belongs to that class can modify its static variables.
    • Also, an instance are not a must to modify the static variable and it can be accessed using the Java class directly.
    • Static variables can accessed by Java instance methods also.
    • When the value of a constant was known at compile time it was declared ' final ' using the ' static ' keyword.

(1) The non-static variables of the class are allocated a memory space by each instantiated object, and the static variable is shared by all instantiated objects of the class in the same block of memory space.

(2) The operation of any one of the instantiated objects of the class on the static variable will change the value of that static variable

(3) Static variables can be accessed directly through the classname.static-variable

(4) Static and final are used to modify member variables and member methods, which can be simply understood as "global constants".

Java Static Methods
    • Similar to static variables, Java static methods is also common to classes and not tied to a Java instance.
    • Good practice in Java are, static methods should be invoked with using the class name though it can be invoked using a N object. Classname.methodname (arguments) or objectname.methodname (arguments)
    • General use for Java static methods are to access the static fields.
    • Static methods can accessed by Java instance methods.
    • Java static methods cannot access instance variables or instance methods directly.
    • Java static methods cannot use the ' this ' keyword.

(1) A static method can be called directly from the class name, and any instance can be called.

(2) A static method cannot use the This and Super keywords, cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static), and can access only static member variables and member methods of the owning class (this and C + + is consistent in the

(3) Because the static method is independent of any instance, the static method must be implemented, not abstract.

Java Static Classes
    • For Java classes, a inner class can be declared using the static modifier.
    • For Java a static inner class it does is not mean, and all their is static. These is called nested static classes in Java

(1) Only inner classes can be declared as static classes

Java keyword--static

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.