Java Review third day-static method

Source: Internet
Author: User

A static method belongs to a class, and memory must allocate memory space for it, which is always occupied by static methods.
The memory manager does not retract the storage space of a static method because the static method is not called, so that if all methods are declared as static methods,
It takes up a lot of memory space, and finally the system slows down. And the normal member method is called by the object,
Memory does not always allocate memory for it, but only when it is called to allocate storage space, and when it is not called,
Storage space will be recalled by memory management, freeing up unused space and increasing the operating rate of the system!

The benefit: Like you said, you don't have to instantiate the call directly through the class name.
Cons: Static method When loading the time has already applied for memory space, do not have to cause unnecessary waste. 、

When using static methods of a class, note that:

A, in a static method can only directly invoke other static members of the same class (including variables and methods), and not directly access non-static members of the classes (this is because, for non-static methods and variables, you need to create an instance object of the class before it can be used, and static methods do not create any objects before use)

Static methods cannot reference the this and Super keywords in any way, because static methods do not have to create any instance objects before they are used, and when a static method is called, the object referenced by this is not produced at all.

Class loading Order in Java

1. When the virtual machine loads the Java class for the first time, it initializes the static initialization block, the static member variable, and the static method once.
2. An instance of the class is created only when the new method is called
3, the class instance creation process: Initializes according to the parent-child inheritance relation, first executes the initialization block part of the parents, then constructs the method of the parent class, then executes the initialization block of the subclass that inherits from this class, and finally the constructor method of the subclass
4, when the class instance destroys, first destroys the subclass part, then destroys the parent class part

Note: The constructor of a subclass, regardless of the constructor with no arguments, will first look for the parent class's constructor without parameters. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.

Java Review third day-static 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.