Java Static keyword

Source: Internet
Author: User

The static keyword in Java is primarily used for memory management (yes, you heard it wrong). We can apply it to variables, methods, blocks of code, nested classes, and import packages. A static keyword belongs to a class, not an instance of a class.

1. Static variables
    1. A static variable can be considered a common property for all objects, such as the employee's company name, the student's school name
    2. Static variables get memory in the method area (class zone) only when the class is loaded
Pros: Save memory 2. Static methods
    1. Static methods belong to classes rather than objects of classes
    2. A static method can be called without an instance of a class, with the implication that a static method can be called by a class, and can naturally be called by a class object.
    3. Static methods can access static data members and change their values
Limitations: There are two main limitations
    1. Static methods cannot use non-static data members or call non-static methods directly
    2. The this or Super keyword cannot be used in a static context because they do not implicitly accept the This parameter
Q&a: Why is the main method of Java static?

Because calling a static method does not require an object. If the main method is a non-static method, the JVM needs to create an object before invoking the main method, which results in additional memory allocations

3. Static code block
    1. Static blocks of code are used to initialize static data members
    2. Static code blocks are executed before the main method is loaded, and can be compared to the execution time of the instance Initalizer block
Q&a: Can we execute a program without the Main method?

No way. The only way before JDK1.6 is to use static blocks of code, but since JDK1.7, he cannot be executed without the main method.

4. Static Inner class
    1. Static inner classes can use static data members and methods of external classes, but not non-static data members and methods. Unlike non-static inner classes, all data members and methods of an external class can be used
    2. Static inner classes are commonly used in build mode
5. Static Guide Package
    1. You can write fewer prefixes to make your code more beautiful.
    2. Do not recommend using

Java Static keyword

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.