About the Java static understanding

Source: Internet
Author: User

First of all, this blog post references the http://www.cnblogs.com/0mbiubiu/p/5331390.html

It took a long time to slowly understand the static, today finally have a time to tidy up a bit.

Static is described here as a member decorated with the static keyword, which includes a statically variable, a static method, and a static block of code.

1. Static and non-static

Always feel that this is a more abstract concept, or to take static variables and non-static variables to explain what is static

Static variables are shared by all objects and have only one copy in memory, which is initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.

The initialization order of static member variables is initialized in the order in which they are defined.

Simply put, the static modified variable when the class is loaded, it has opened up a memory space to save it, and it is unique, in memory is only one, not a static object, each time new opens up a piece of memory space.

  

2.static method

Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called.

Simply put, you do not need a new class to directly call a method

public static void F () {...};

3.static code block

Another key function of the static keyword is to form static blocks of code to optimize program performance. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once.

Code blocks that are automatically loaded when the class is first loaded

static{StartDate = date.valueof ("1946"); EndDate = date.valueof ("1964");}

Static members of a class (variables and methods) belong to the class itself, which allocates memory when the class is loaded, can be accessed directly through the class name, and non-static members (variables and methods) are objects of the class, so memory is allocated only when the class's object is generated (an instance of the class is created) and then accessed through the object (instance)

Accessing its non-static members in a static member of a class is an error because a static member of the class already exists when the non-static member of the class does not exist, and it is of course an error to access something that does not exist in memory:

  

About the Java static understanding

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.