Static keyword, raising the spring common class to get spring's bean thinking

Source: Internet
Author: User

The static keyword is used in C + + and Java to decorate statically member variables and member functions

An example of an ordinary JavaBean

class aa{   int  A;    Static int b;   Geta/seta; // omit Getset here   getb/Setb;}


If you create an object AA,

AA a =new aa ();

This time only allocates four bytes to this object in memory, which is the number of bytes of the A variable, because the global variables of the static declaration are common to all objects of this class in the global zone, for example:

A.setb (Ten=new  A (); System.out.println (B.getb ());

The findings were 10;

It also confirms that the key word for the static declaration is that all objects are common.

This also explains why static functions can only use static variables, because static functions are common to all classes, and the compiler does not pass references to the class to this method when it is compiled.

So the static function doesn't know which object called me this way.

So where does the normal function exist, and why does the function know which object called me when the object calls this method?

Because the normal member variables and methods in the compiler compile time, will help us to do the conversion.

Or the class above, at compile time, the compiler will turn the class into:

class aa{    int  A;      Public void SetA (AA *ac,int  a)    {       ac->a=A;        }}    


The address of the current class is passed over, modifying the value of the object A. (The above code is implemented in C + +, I think the Java bottom is also implemented this way)

The way to invoke the static Statics function is included in Java (Java)

1 class name. static method ();

2 object. static method ();

Common Java class methods to get the classes of the spring container:

 Public classSpringconfigtoolImplementsApplicationcontextaware {//extends//applicationobjectsupport{    Private StaticApplicationContext context =NULL; Private StaticSpringconfigtool stools =NULL;  Public synchronized Staticspringconfigtool init () {if(Stools = =NULL) {Stools=NewSpringconfigtool (); }        returnstools; }     Public voidSetapplicationcontext (ApplicationContext applicationcontext)throwsbeansexception {Context=ApplicationContext; }     Public synchronized StaticObject Getbean (String beanname) {returnContext.getbean (beanname); }}
<id= "Springconfigtool"  class= " Com.nfcm.spring.SpringConfigTool "/>


This will set the spring context object to the global variables of our custom springconfigtool when the spring container runs, and we know that the global variables are common to all classes,

So we can use Springconfigtool.getbean directly ("Beanname"), or the new object calls Getbean to fetch the bean corresponding to spring.

Static keyword, raising the spring common class to get spring's bean thinking

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.