Java Static keyword Explained

Source: Internet
Author: User
Tags create domain

think of two things first:1. Why use the static keyword? 2. What is the difference after adding the static keyword modifier? Static

I. Why should I use 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.

Two. What is the difference between adding the static keyword modifier?

1.this cannot call an attribute field that is modified by static.

Because the static modifier is initialized when the class is loaded, it is called a class-level variable (belonging to the Class), and the instance of the class is initialized at run time and belongs to the object-level variable (belonging to the object).

This refers to an instance of the current object, and super refers to an instance of the parent class object. This (super) cannot invoke a static decorated property field because the JVM has a class loader that executes the static domain the first time it is loaded, and the JVM specifically divides a memory area into a static block. So whenever you can call this static Modified property field or method, belongs to the class. The this pointer is an object to the class that the JVM assigns to a concrete object when the object is instantiated, and this points to the object. Therefore, a non-static property or method cannot be called in a static method , and if the This keyword is used in a static method, this cannot point to the appropriate object. (In layman's words: The JVM's class-loading mechanism determines that loading static is first and non-static, that is, non-static properties and methods do not exist when loading static)

2.static Modified Properties and methods

1.static modifies properties, that is, all such objects share a single copy of the storage space.

 Public classStatictest { Public Static intStatica;  Public intA;  Public Static voidMain (string[] args) {statictest test; Statictest A1=Newstatictest (); Statictest A2=Newstatictest (); A1.statica++; A2.statica++; A1. A++; A2. A++; System.out.println ("Staticainfo:" +a1.statica+ "" +A2.statica); System.out.println ("Ainfo" +A1. A + "" +A2.    A); }}/**Run result * Staticainfo:2 2 * AInfo1 1 **/

That is, static modifies not all of a specific object, but all objects of that class are common, and Statictest.statica has only one copy of storage space.

2.static retouching methods, classes and objects can call the method, and methods that are not static decorated can only be called by an instantiated object.

 public  class   statictest { public  static  void   Statictest () {}  public  void   Test () {}  public  stat       IC  void   main (string[] args) {       Statictest.statictest ();   // statictest.test ();       The call is error  statictest test=new   Statictest ();       Test.statictest ();    Test.test (); }} 

The static modifier member method has the greatest effect of using the "class name. Method Name" method to avoid the tedious and resource consumption of the first object, a static decorated class that cannot use non-static modified member variables and methods, which is well understood, Because the static modification method belongs to the class, if you go directly to use the object's member variable, it will be overwhelmed (I don't know which object's properties to use).

The biggest benefit of the static modifier member method is that it saves the overhead of the new object, which means that the method can be called directly from the class.

3.static decorated inner class

It is good to understand that a few of the less common uses can only be used to decorate inner classes, a static decorated inner class, or non-static decorated member variables and methods, because the static modification method belongs to the class, and if you go directly to the object's member variable, It will be overwhelmed (I don't know which object's properties to use).

4. Static block
classbook{ PublicBook (String msg) {System.out.println (msg); }} Public classPerson {book Book1=NewBook ("BOOK1 member variable Initialization"); StaticBook Book2 =NewBook ("Static member BOOK2 member variable Initialization");  PublicPerson (String msg) {System.out.println (msg); } Book Book3=NewBook ("BOOK3 member variable Initialization"); StaticBook BOOK4 =NewBook ("Static member BOOK4 member variable Initialization");  Public Static voidfunstatic () {System.out.println ("Static modified Funstatic method"); }         Public Static voidMain (string[] args) {person.funstatic (); System.out.println ("****************"); Person P1=NewPerson ("P1 initialization"); }    /**Output * Static member BOOK2 member variable initialization * Static member BOOK4 member variable initialization * Static modified Funstatic method * *************** * bo OK1 member Variable initialization * BOOK3 member variable initialization * P1 initialization*///~}

In the above example we can find two interesting places, the first is when we do not create the object, but through the class to invoke the class method, although the method does not use any of the class Members, class members or before the method call initialization, which means that when we first go to use a class, The member initialization of the class is triggered. The second is that when we use the class method, after initializing the members of the class, and then new to the object of the class, the static decorated class member is not initialized again, which means that thestatic decorated class member, in the process of running the program, only need to initialize once, and will not be initialized multiple times .

classbook{ PublicBook (String msg) {System.out.println (msg); }} Public classPerson {book Book1=NewBook ("BOOK1 member variable Initialization"); StaticBook Book2; Static{Book2=NewBook ("Static member BOOK2 member variable Initialization"); Book4=NewBook ("Static member BOOK4 member variable Initialization"); }         PublicPerson (String msg) {System.out.println (msg); } Book Book3=NewBook ("BOOK3 member variable Initialization"); StaticBook Book4;  Public Static voidfunstatic () {System.out.println ("Static modified Funstatic method"); }         Public Static voidMain (string[] args) {person.funstatic (); System.out.println ("****************"); Person P1=NewPerson ("P1 initialization"); }    /**Output * Static member BOOK2 member variable initialization * Static member BOOK4 member variable initialization * Static modified Funstatic method * *************** * bo OK1 member Variable initialization * BOOK3 member variable initialization * P1 initialization*///~}

Java Static keyword Explained

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.