The role of Static in Java

Source: Internet
Author: User

The static keyword has two main functions: assigning a single storage space to a particular data type or object, regardless of the object being created; Implementing a method or property is associated with a class rather than an object-that is, you can call a method directly or use the properties of a class without creating an object.

1). Static member variable: statically variable belongs to class, only one copy in memory (created once in memory when the class is created), as long as the static variable is loaded, the static variable is allocated space, so it can be used. There are two ways to reference a static variable, namely, "class. Static variable" and "object. Static variable", and static variable is shared by all objects; An instance variable belongs to an object, and only when the object is created does the instance variable be allocated memory space to be used, it has multiple copies in memory, and is referenced only by the "object. Instance variable" method.

2). Static Member Method: The static method and the non-static method are available in Java. The static method is a method of a class that can be called without creating an object, and not as a method of an object, but only when the object is created. The this and Super keywords cannot be used in the static method, cannot call a non-static method, can only access static member variables and member methods of the owning class, because when the static method is called, the object of this class may not have been created, even if it has already been created. There is no way to determine which object to invoke . Similarly, a static method cannot access a variable of a non-static type.

3). Static code block: A static code block is a block of code that is independent of member variables and member functions in a class. Note: These static blocks are executed only once, and the static code block executes when the JVM loads the class, and if there are multiple static blocks of code, the JVM executes in order.

4). Static inner class: The static inner class refers to an inner class that is declared static, which can be instantiated without relying on an external class instance object, and the usual inner class needs to be instantiated outside of the instance.

5). Static and final use: For variables, if you use the static final decoration, which means that once the assignment cannot be modified and is accessible through the class name, for a method, the static final decoration, if used, means that the method is not overwritten and can be accessed directly through the class name.

The role of Static in Java

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.