Basic knowledge of Java static

Source: Internet
Author: User

First, the use of static

   in java programming idea has such a sentence:" static method is that there is no this static Method cannot call a non-static method inside, which in turn is possible. You can also call static method. This is actually the static < span>

In my understanding, this is equivalent to the static method belonging to the class, not the static method belonging to the object. In practice, the equivalent of a non-static method is called only if the corresponding object is in new , and the static method only needs the ' class name ' . method ' can be called.

Under what circumstances do we need to use the static keyword?

A, for a class of relatively fixed some parameters and methods

b, some parameters and methods that need to be pre-defined

C, some call a wide range of public parameters and methods

In my opinionStaticthe core of the keyword is to avoid some memory duplication, and we imagine aJavawhen the project starts to run, it passesJVMallocate a space in which all the required classes are preloaded, and then weNewThe object is constantly occupying memory. However, for example, enumeration classes(later called Contactclass)the parameters are called in many other objects and functions, and if we need to restart each time we callNewa Contactobject, it will not be repeated in memory to occupy a very many resources. At this point, we needStatickeyword retouching Contactclass, so that when we pre-load all of the classes, Contactclass through theStaticthe modified parameters are then Contactclasses are loaded together in memory and can be passed directly when we need them.Contact .parameter name to call this parameter without having to reuse additional memory. (If you are asked in the interview, the answer should be very extra points)

1. Modifying class variables

Static variables are also commonly referred to as static variables, and the difference between a statically variable and a non-static variable is that a static variable is shared by all objects and has 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.

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

The following examples illustrate:

This is a basic man class and generates two objects, but from the definition of the class, we can assume that the value of the sex parameter is fixed to a male, but generating two objects is equivalent to having a sex parameters are stored two times, which is undoubtedly a waste of memory. And what happens when we add the static keyword to the sex parameter ?

We only need to assign a value to the sex parameter in the man class to change the sex value of all the man objects .

Although the sex parameter becomes a shared property of the man class, it is seldom used in practice, because it makes the parameter difficult to control because it can be changed everywhere. So we generally adopt other methods of use:

Passprivate+staticRetouchingCountproperty, and inMansin the constructor of theMansCount the number of objects and assign values to theIDproperty to distinguish between the differentMansobject. And becausePrivatedecoration cannot be changed arbitrarilyCountand theIDparameters.

2. Method of modifying class

The same as the static parameter usage scenario, but it is important to note that the same class, static methods cannot directly invoke non-static parameters and non-static methods, but non-static methods can be directly called static methods and static parameters.

3. Static code block

The main function of a static code block is to perform some data processing before the class is loaded, initializing the required parameters or objects. Guarantee the next use of the parameter or object.

This example shows some basic functions of static code blocks, and we can look at this example to show the loading order of static blocks and objects when they are initialized:

4, static guide package

Static Guide package is a relatively rare is the use of static, and then use an example to illustrate it:

When we use the import static staticpkg.man.*; , we can use it directly in this class without conflict with the method name. Mans in the class Static method, just like this is your own method. But the corresponding other static parameters and the non-static parameters and non-static methods can not be called directly.

Second, the static effect analysis

1, before said that static is with the class to load, then we can use this to make static parameters call it?

We can see that although the static parameters are loaded with the class, it is possible to use this for parameter invocation in case of a conflict with the parameter name to differentiate between global variables and local variables.

2, Static is followed by the class to load, then with the class, foundation, object initialization process of the corresponding loading sequence is what?

Example 1:

What will be the output of these two pieces of code?

We said it.Staticis loaded along with the class, wenew Test3 ()when it loadsTest3class found at this timeTest3InheritanceTest2then we need to load it first .Test2class, and then load the static code block of theTest3the static code block for the class. After the class load is complete, we begin to passTest3provide the constructor to come todayTest3object is initialized, and becauseTest3inherited theTest2so at compile time by defaultTest3In the constructor, there is aSuper ()the call, meaning to load firstTest2the constructor reloadsTest3constructor for the device. So the output of these two pieces of code is as follows:

Example 2:

What should be the loading order of these three pieces of code?

First and example1sameMainLoad in functionTest3class, but because inheritanceTest2so first loadTest2class, and then the static code block of theTest3static code block, and then start loadingTest3constructor, also because the inheritance needs to be loaded firstTest2constructor, but you need to initialize theTest2object, you need toTest2The global variables in the class are loaded on a row-by-line basis, we need to loadNew Test4 ("test22222222"), then you need to loadTest4class, the executionTest4class, and then executes the static code block of theTest4the constructor,Test4object loading is complete,Test2class is loaded, and then the constructor of theTest3class constructor is loaded, as you need to load firstTest4class, becauseTest4the static code block of the class has been loaded without having to load repeatedly, so loadingTest4constructor, and then load theTest3constructor, so the output of these three pieces of code should be:

Example 3:

Why does it still print when it's not written in the Main function?

Since the main function is loaded with a row-by-line load of the Test5 class, two static blocks of code are loaded from top to bottom, and after the load is completed, this class is loaded to finish the execution of main The code in the function, the result is no code in the main function, the program ends.

These are some of my basic knowledge of static , I hope to help you, the code is still a picture, code or hand-knocking to try to feel more.

Reference blog:

Https://www.cnblogs.com/dotgua/p/6354151.html?utm_source=itdadao&utm_medium=referral

Http://www.cnblogs.com/dolphin0520/p/3799052.html

Basic knowledge of Java static

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.