Static Usage Analysis-java language

Source: Internet
Author: User

 

The data defined in the class becomes the data member of the class, such as fields and constants. Function member methods provide data operations, such as function member methods, attributes, and constructors. Data members and methods in objects are generally private objects, that is, only objects can be accessed, and other objects cannot directly operate on them. However, multiple instances need to be generated if called in multiple places. Sometimes the called method has nothing to do with the number of instances. This method may only be a helper method. In this case, there is no need for multiple instances. java introduces static. first look at an instance:

Public static void main (String [] args ){

System. out. println (Math. random ());

 

}

In jdk, the random method of Math is used to generate random numbers. It is just a help method and has no relationship with the instance variables in Math, therefore, you do not need to call an instance to generate an instance at a time. You only need to call the instance directly through the class. In this JDK, the random static (static) method is provided.

Static not only can be used to modify attributes, but also does not have any relationship with the number of instances, static methods and attributes can be shared among multiple instances.

See the following example:

Public class Text {

Static int count = 0;

Public StaticCount (){

Count ++;

}

Public static void main (String [] args ){

StaticCount count1 = new StaticCount ();

StaticCount count2 = new StaticCount ();

System. out. println ("count =" + count );

}

}

Running result: count = 2

From the above example, we can see that the objects pointed to by count1 and the objects pointed to by count2 share static variables. In fact, the entry function we often mention is also a static method. The static method has no relationship with the instance. It can directly call static variables. You can also call static attributes or methods through instance references. The results are the same, but they are not needed.

You can modify a piece of code directly through static. See the following example:

Public class Text {

Int count;

Static {

System. out. println ("in the static segment ...");

}

Public Text (){

System. out. println ("in the constuctor segment ...");

}

Public static void main (String [] args ){

Text sd0 = new Text ();

}

}

Running result:

In the static segment...

In the constuctor segment...

 

We have noticed that the code block modified by static is executed before the constructor. In fact, the static code block is executed during program loading, the constructor is executed during running. The static code block is loaded only once. See the following example:

Public class Text {

Static {

System. out. println ("in the static segment ...");

Public Text (){

System. out. println ("in the constuctor segment ...");

}

Public static void main (String [] args ){

Text sd0 = new Text ();

Text sd1 = new Text ();

Text sd2 = new Text ();

}

}

 

Running result:

In the static segment...

In the constuctor segment...

In the constuctor segment...

In the constuctor segment...

The running result shows that the static code is executed only once, and the constructor executes three times, that is, the static code block will

It is executed only once during loading.

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.