Static keyword (ii)

Source: Internet
Author: User

The last static keyword, self-think more detailed. Do not know can leave a message. The following goes on:

static Final member variable:

is a member variable of a class that is both final and static decorated. It is similar to a member variable that is only final decorated . In addition to being initialized at the time of definition or at the time of first declaration, it can only be initialized in a static block. (attention to the words I marked with green)

Then we learn a little bit about the following two situations, we remember good:

1: Initialization problem with only final modified member variables

(1) When the first definition of the variable to be final modified, it is initialized;

(2) If a final modified member variable is first defined, it is not initialized, then the constructor of the related class is overloaded, and then the constructor is assigned a value for the final decorated member variable.

(3) A member variable that is final modified equals a constant whose value cannot be changed .....

2: Characteristics of modified member variables by static

only a piece of memory resources, no copy, in the previous article is very detailed.

3: A member variable that is both static and final modified (once assigned, the value cannot be changed and there are two ways of the variable)

at the same time by the two key words we do not fear, one by one to see.

In other words: two ways to access member variables. First time: class name; object;

Also: Initialize the problem (

<1> at the time of definition, initialize;

<2> initialization in a static block of code;

▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

The ▇<3> is defined in the constructor. Let's try. ▇

▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

(with a lot of black graphics around the <3> is wrong, I think of it, the results through validation is wrong, see the article at the end of the place )

Corresponding <2> examples used in static code blocks

public class ClassInfo3
{
public static int number;//class number
public static final String classdesc;//not initialized

Static code block
static{
Classdesc = "class description";
}

Public ClassInfo3 () {
int n = 0;
n =number + +;
SYSTEM.OUT.PRINTLN (n);
}

public void Shownumber () {
SYSTEM.OUT.PRINTLN (number);
}

}


public class ClassInfoDemo3
{
public static void Main (String args[]) {
ClassInfo3 C1 = new ClassInfo3 ();//Generate Instance 1.println (0,1);
C1.shownumber ();//
System.out.println (C1.CLASSDESC);
System.out.println (CLASSINFO3.CLASSDESC);
System.out.println ("***************** above is the test data of Example 1 ***************");
System.out.println ("");
ClassInfo3 C2 = new ClassInfo3 ();//println;
C2.shownumber ();
System.out.println (C2.CLASSDESC);
System.out.println (CLASSINFO3.CLASSDESC);
}
}

This is run:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/22/wKiom1WSW22TNXhmAAG09SNLNQE638.jpg "title=" QQ picture 20150630170240.jpg "alt=" Wkiom1wsw22tnxhmaag09snlnqe638.jpg "/>

3: We test the third case and change it slightly in the (2) code

Here's the code:

/* This is a commented code
    static{
     CLASSDESC = "class description";
    }
    */

    public ClassInfo4 () {
        //First assigns a value to a static member variable
          Classdesc = "Sunny 1204 class";
         int n = 0;
          n =number  + +;
         System.out.println (n);
    }

    public void Shownumber () {
      System.out.println (number);
    }

}


public class ClassInfoDemo4
{
public static void Main (String args[]) {
ClassInfo4 C1 = new ClassInfo4 ();//Generate Instance 1.println (0,1);
C1.shownumber ();//
System.out.println (C1.CLASSDESC);
System.out.println (CLASSINFO4.CLASSDESC);
System.out.println ("***************** above is the test data of Example 1 ***************");
System.out.println ("");
ClassInfo4 C2 = new ClassInfo4 ();//println;
C2.shownumber ();
System.out.println (C2.CLASSDESC);
System.out.println (CLASSINFO4.CLASSDESC);
}
}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/21/wKioL1WSiCTh61IWAAG86V31L_A900.jpg "title=" QQ picture 20150630200732.jpg "alt=" Wkiol1wsicth61iwaag86v31l_a900.jpg "/>

As you can see, the final final variable with the static modifier cannot be assigned to (static final variable) by a constructor.

So you can summarize the points:

about static final variables: The statics representation is a class member variable, there is no copy, there is only one resource that can be accessed in two ways, and the final modifier means that there are constants that can be accessed in two ways.

Note here: Initialization of the static final variable: (1) When the variable is defined, initialize, (2) if it is not initialized at first, then it is necessary to construct a static code block to assign a value to it.



The writing is a bit messy, but always understand. The use of static and final. If you do not understand, welcome message: Learn together, progress together. Java is a very good language, love Java, love life.



Static keyword (ii)

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.