The role of the Java static modifier

Source: Internet
Author: User

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 to release the space, that is, as long as the program is running, then this memory will persist. What is the point of doing this?
In a Java 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 would normally have to instantiate the object before you can access those members by reference to the object, but there is an exception. Is that the member is declared with static (the access control of the class is excluded here), for example:
Not declared as static
Class classa{
int b;
public void Ex1 () {
...
}
}
Class classb{
void ex2{
int i;
ClassA a = new ClassA ();
i = a.b; This is where the member variable b is accessed through the object reference
A.ex1; This accesses the member function through the object reference Ex1
}
}

Declared as static
Class classa{
static int b;
static void Ex1 () {
...
}
}
Class classb{
void ex2{
int i;
i = classa.b; This is where member variable B is accessed through the class name
Classa.ex1; This is where the member function is accessed through the class name Ex1
}
}
Through the above two comparisons, you can know that static is used to modify the class members of the main role, in the Java class Library, many of the class members are declared static, you can let the user do not need to instantiate the object can refer to the member, the most basic integer.parseint (), Float.parsefloat () and so on are used to convert objects to the basic data type you need. Such variables and methods are also called class variables and class methods.
Next talk about the value of the variable after the static modification of the problem, just described earlier, the member after the static modification, at compile time by memory allocation of a piece of memory space, until the program stops running to be released, so that all objects of the class will share this memory space, 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 (); Ditto
System.out.println (T.I);
System.out.println (TT.I);
System.out.println (T.I);
}
}

The role of the Java static modifier

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.