The role of Static in Java

Source: Internet
Author: User

Staticis static modifier, what is static modifier? As you know, any variable or code in the program is automatically allocated by the system at compile time to store the memory, and so-called static refers to the memory allocated after compilation will persist until the program exits the memory will release the space, that is, as long as the program is running, then this memory will always exist. What is the point of doing this?
InJavainside the program, everything is an object, and an object's abstraction is a class, and for a class, if you want to use his members, you have to instantiate the object before you can access them by reference to the object, but there is a case in which the member isStaticdeclared (where the class's access control is excluded), for example:
Not declared asStatic
Class classa{
int b;
public void Ex1 () {
...
}
}
Class classb{
void ex2{
int i;
ClassA a = new ClassA ();
i = a.b; //this is where member variables are accessed through object referencesb
A.ex1; //this is where member functions are accessed through object referencesEx1
}
}

Declared asStatic
Class classa{
static int b;
static void Ex1 () {
...
}
}
Class classb{
void ex2{
int i;
i = classa.b; //member variables are accessed through the class name hereb
Classa.ex1; //member functions are accessed through the class name hereEx1
}
}
Through the above two comparisons, you can knowStaticis used to modify the primary role of a class member,Javamany class members in a class library are declared asStatic, which allows the user to refer to a member without having to instantiate the object, the most basicInteger.parseint (),float.parsefloat ()and so on to convert the object to the basic data type you want. Such variables and methods are also called class variables and class methods.
Next talk about beingStaticThe question of the value of the modified variable, just before, wasStaticA modified member that allocates a chunk of memory at compile time, until the program stops running before it is freed, so that all objects of the class share this memory space, take a look at the following example:
Class tstatic{
static int i;

Public tstatic () {
i = 4;
}

Public tstatic (int j) {
i = j;
}

public static void Main (String args[]) {
Tstatic t = new tstatic (5); //declares an object reference, and instantiates
Tstatic tt = new tstatic (); //Ibid .
System.out.println (T.I);
System.out.println (TT.I);
System.out.println (T.I);
}
}
This code insidetstaticclass has aStaticof theintvariablesI, there are two constructors, the first of which is used to initialize theIto be4, and the second one is used to initializeIis the value passed into the function,MainThe value passed in is5, the program first declares a reference to the objectT, and then invokes the constructor with parameters to instantiate the object, at which point the objectTthe member variableIthe value is5, and then declare the objectTT, call the parameterless constructor, then put the objectTTthe member variableIvalue is initialized to4, attention, here .Iis aStatic, which means that all objects of the class share the memory, that is, the object is instantiatedTTchanges in time.Ivalue, then actually the objectTof theIThe values also change because they are actually referencing the same member variable. The last printed result is three4. Hehe, do you understand this? If you don't understand, just take a good look at the book.


This article is from the "10943701" blog, please be sure to keep this source http://10953701.blog.51cto.com/10943701/1751862

The role of Static in Java

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.