Detailed Java static keywords

Source: Internet
Author: User
Tags stub create domain

Mention the static keyword, I believe that we will never be unfamiliar, but, want to fully understand, fierce a think, found himself and said not quite understand ... For example, yesterday when asked by a classmate ... Of course, not everyone is as good as I do, but the foundation of such a weak person should be a lot, because commonly used, so with everyone, but when it comes to fine places are very tough. This blog is I turned out my original study notes together with some of my own blog to sort out, for the basic knowledge of the students reference.

1. Problems to be solved by the static keyword

Here's an excerpt from the text of the static keyword in the Java Programming Idea (Fourth edition): (P29) Typically, when a class is created, it is the appearance and behavior of the object that describes that class. Unless you create that object with new, you do not actually get any objects. When you execute new to create an object, the data storage space is allocated and its methods are called by the outside world. There are two scenarios that cannot be solved with the above method. One scenario is that you just want to allocate a single storage space for a particular domain, without thinking about how many objects you want to create, or even need to create any objects at all. Another scenario is that you want a method not to be associated with any object that contains his class. That is, the method can be called even if the object is not created . In simple terms, the main purpose of static is to create domain variables and methods that are independent of the specific object.

2. Loading time for a static modified variable or method or class

Add the part of the static adornment while loading the class. ( Note: There are no specific objects at this time, and this process only takes place once )

3. Use code examples to see the static variables, static methods, static class effects 3.1 static variables
 Public classstatictest{ Public Static intCount =0; @SuppressWarnings ("Static-access")     Public Static voidMain (string[] args) {//TODO auto-generated Method StubStatictest test1 =Newstatictest ();        System.out.println (Test1.count); Statictest test2=Newstatictest (); Test2.count++; System.out.println (Test1.count+ "" +test2.count+ "" +statictest.count); }}

Output Result:

    1    1

It can be seen that the static variable is not owned by a specific object of the class, but is common to all objects of the class, and static variables can be called by the object as well as directly by the class.

In addition, static variables cannot refer to non-static methods, as described in the preceding static loading time, when loading static, non-static variables, methods and so on do not exist, of course, can not be referenced. However, a non-static method or class can normally refer to a static variable or method. Because non-static is always present after the static.

3.2 Static methods

Static methods, like static variables, are owned by the class, executed at the same time as the class is loaded, not part of a specific object, and can be called by all objects. The following points need to be noted for static methods:

    • They can only invoke other static methods.
    • They can only access static data.
    • They cannot refer to this or super in any way.
class Simple {    staticvoid  Go () {       System.out.println ("Welcome");      Public class Cal {    publicstaticvoid  main (string[] args) {       simple.go ( );    }}

Static methods are generally used in tool classes, and can be used directly by invoking tool methods with the class name.

3.3 Static Classes

In general, an ordinary class is not allowed to be declared static, but it can be declared as static in an inner class, at which point the outer class can call the inner class directly, because the inner class of static is loaded at the same time that the outer class is loaded, so to say, Do not instantiate an external class to invoke static internal classes directly. See Example:

 Public classbasestatic {Static{System.out.println ("Load base Static"); }     Publicbasestatic () {System.out.println ("Basestatic Constructor"); }        Static classbaseinnerclass{Static{System.out.println ("Base Inner class Static"); }                 PublicBaseinnerclass () {System.out.println ("Baseinnerclass Constructor"); }    }} Public classstaticloadordertest{ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        NewBasestatic.baseinnerclass (); }}

Before you look at the answer, think about what the output is.

Before we look at the answer, let's take a look at the execution process: first, load the Staticloadordertest class before entering the main method of Staticloadordertest, and then execute the new Basestatic.baseinnerclass (); Here's a note: Because Baseinnerclass is static, there's no need to load external classes and instances, and you can load baseinnerclass and instantiate them directly. So the output:

Base Inner class static
Baseinnerclass Constructor

Here's a pit: When using the external class name directly. When the static inner class is instantiated, if the external class is not loaded (and actually not loaded), then this statement: What is the existence of basestatic in Basestatic.baseinnerclass???? Is it just a simple name association with a static inner class? What if you design static inner classes like this? I don't think Java designers are making this mistake, are they? may also be because of the JVM is not familiar with, for the bottom is not very understanding, if the passing of the great God can help solve, thank you!!!!

3.4 Example of static load order

What is the output of the following code?

 Public classbasestatic {Static{System.out.println ("Load base Static"); }         Publicbasestatic () {System.out.println ("Basestatic Constructor"); }        Static classbaseinnerclass{Static{System.out.println ("Base Inner class Static"); }                 PublicBaseinnerclass () {System.out.println ("Baseinnerclass Constructor"); }    }} Public classStaticloadordertest {Static{System.out.println ("Load Test"); }         Publicstaticloadordertest () {System.out.println ("Test Constructor"); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        Newbasestatic (); Newstaticloadordertest (); NewBasestatic.baseinnerclass (); }}

As above, analyze the process: Before entering the Main method, the Staticloadordertest class needs to be loaded, when a static block of code is found, the code block is loaded first, and then the inside of the main method is entered, new Basestatic (), The Basestatic class needs to be loaded at this point, and the static code block is loaded first, and then the constructor is called. Note: This does not load baseinnerclass, because it is the internal class only when the actual use of the time will be loaded, I believe the smart reader see this is not a single example of the implementation of the design mode? Study it yourself. Back to the main method, it is time to execute new Staticloadordertest (), because the Staticloadordertest class has been loaded once, and the class is loaded only once, so it is constructed directly, and then the last sentence, new Basestatic.baseinnerclass (), as in the example above, is no longer detailed here. So the output is:

StaticclassstaticBaseinnerclass constructor

Think again, if I change the example above to the following, what will the output be?

 Public classbasestatic {Static{System.out.println ("Load base Static"); }         Publicbasestatic () {System.out.println ("Basestatic Constructor"); }        Static classbaseinnerclass{Static{System.out.println ("Base Inner class Static"); }                 PublicBaseinnerclass () {System.out.println ("Baseinnerclass Constructor"); }    }} Public classStaticloadordertestextendsbasestatic{Static{System.out.println ("Load Test"); }         Publicstaticloadordertest () {System.out.println ("Test Constructor"); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        NewBasestatic.baseinnerclass ();Newstaticloadordertest (); 
new basestatic (); }}

The above is some knowledge of the static keywords in java.

Finally, I wish the first replace him of the top four races victory!????????

  

Code word is not easy, please respect the original, reproduced please indicate the source.

Resources:

http://yongliang567.iteye.com/blog/904467

http://blog.csdn.net/anmei2010/article/details/4096131

http://blog.csdn.net/brouth/article/details/51656603

"Java Programming Idea (fourth edition)"

Detailed Java static keywords

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.