1. Static grammar rules

The static modifier is used to modify member variables and methods. Note the following:

  1. Use the static modifier to indicate that the member (property or method) is static;
  2. Static members belong to the class itself, not to a concrete object;
  3. Only one static member exists in memory. All objects of this class share this static member, that is, access the same memory block.
  4. Static members can be passed directly through the class name. Static variable names can be accessed directly or through objects;
  5. If static members are private, the outside world is still not directly accessible (by class name).

The access relationship between static members and static attributes is as follows:

  • Non-static methods can have static attributes and methods;
  • Static methods cannot access non-static properties and methods.

2. Example — Count how many objects a class produces

/ * * *@briefUse the static variable to count the total number of objects produced by this class *@author  mculover666
 * @date2019/4/26 * * /
class A
{
    /* private set to private to prevent external access */
    private static int count = 0;

    public A(a)
    {
        // Count increments each time an object is generated
        count++;
    }

    /** * gets the number of currently generated objects * 1. Static method, accessed by class name * 2. Public, externally accessible anywhere */
    public static int getCnt(a)
    {
        returncount; }}class TestStatic1
{
    public static void main(String[] args) {

        /* Create 5 objects */
        A aa1 = new A();
        A aa2 = new A();
        A aa3 = new A();
        A aa4 = new A();
        A aa5 = new A();

        /* Outputs the current number of generated objects */
        System.out.println("Class A is created."+ A.getCnt() + "An object."); }}Copy the code

The running results are shown as follows:

3. Example — a design method in which only one object can be produced from a class

/ * * *@briefUse the static modifier to make a class produce only one object *@author  mculover666
 * @date2019/4/26 * * /
class A
{
    // If only one object is generated for subsequent testing
    public int i = 10;
    
    /** * 1. Use the default constructor to generate an object; * 2. Modify to private, cannot be accessed externally; * 3. Render static. There can only be one object in memory. * * /
    private static A aa = new A();

    /* The constructor is modified to be private, and new objects can no longer be generated externally using new A() */
    private A(a)
    {
        System.out.println("A class A object is generated.");
    }

    /** * 1. Provide A method to get class A objects; * 2. Modify to public, accessible from anywhere outside; The aa attribute is static and can only be accessed by static methods. * 4. Return type A, return the address of class A aa; * /
    public static A getA(a)
    {
        returnaa; }}class TestStatic2
{
    public static void main(String[] args) {
        
        /** * 1. The default constructor A() is private, so you can't use new A() to get objects; * 2. Use the static getobject method getA() provided by class A; * /
        A a1 = A.getA();
        A a2 = A.getA();

        /** * start test a1 and a2 refer to the same object * 1. I default value is 10; * 2. Change the value of a1 object I and check whether the value of A2 object I changes */
        a1.i = 99;
        System.out.println("a1.i = " + a1.i);
        System.out.println("a2.i = "+ a2.i); }}Copy the code

The running results are shown as follows:

Welcome to subscribe to my wechat official account: “McUlover666”.