Static usage in Java (i) statically member variable __java

Source: Internet
Author: User

Static can modify member variables, methods, free blocks, inner classes.

Static-Decorated member variables: the static-decorated member variable is also called a class or global variable, and the static-decorated member variable is initialized when the class is loaded, and is associated with the class, as long as the class exists and the static variable exists. A static variable divides a single storage space and is not bound to a specific object, which is shared by each object of the class. This means that when an object is declared, it does not produce a copy of the static variable, but all instance objects of that class share the same static variable. A static-decorated member variable can be accessed before any instance object is created without reference to any object, that is, no matter how many objects are created, the static-decorated variable occupies only one memory.

A reference to a static-decorated member variable:

Can be referenced through instance object names and class names, and can be invoked directly in the same category without the need for object names and class names.

Look at the example below:

Package com.baiye.test;

public class Statictest {
static int a=9;
Static String str= "Quan";
Public Statictest () {
System.out.println ("The static variable is initialized when the class is loaded (before the object is instantiated): A=" +a);
a=4;
System.out.println (a);
System.out.println (str);

}
static void Staticmethod () {
Str= "Baiye";
return str;
SYSTEM.OUT.PRINTLN ("Reference static variable in static method: Str=" +str);
}
void Generalmethod () {
a=6;
SYSTEM.OUT.PRINTLN ("Call static variable in Non-static method: A=" +a);
}
public static void Main (string[] args) {
Statictest st1=new statictest ();
Statictest st2=new statictest ();
Statictest st3=new statictest ();
st1.a=5;
System.out.println ("st1.a=" +st1.a+ "st2.a=" +st2.a+ "st3.a=" +st3.a);
Staticmethod ();
System.out.println ("st1.str=" +st1.str+ "st2.str=" +st2.str+ "st3.str=" +st3.str);
Statictest st4=new statictest ();
St1.generalmethod ();
System.out.println ("st1.a=" +st1.a+ "st2.a=" +st2.a+ "st3.a=" +st3.a);
}

}

Output results:

The static variable is initialized when the class is loaded (before the object is instantiated): a=9
9
Quan
The static variable is initialized when the class is loaded (before the object is instantiated): a=9
9
Quan
The static variable is initialized when the class is loaded (before the object is instantiated): a=9
9
Quan
St1.a=5 st2.a=5 st3.a=5
Referencing static variables in static methods: Str=baiye
St1.str=baiye St2.str=baiye St3.str=baiye
The static variable is initialized when the class is loaded (before the object is instantiated): a=5
5
Baiye
To call a static variable in a Non-static method: A=6
St1.a=6 st2.a=6 st3.a=6

In this result, we can see that no matter how many objects in the instance are static decorated, the member variable occupies only one

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.