Java Notes 17. static and final usage in java,. javastatic

Source: Internet
Author: User

Java Notes 17. static and final usage in java,. javastatic
In the last few blog posts, we know that when we write a class, we are actually describing the attributes and behaviors of the Class Object, without generating a substantive object, the system allocates memory space to the object only when the new keyword is used to generate the object. The method can be called externally. However, sometimes we want to have only one copy of some specific data in the memory space no matter whether an object is generated or no matter how many objects are generated, in this case, the static keyword is used.
Reprinted please indicate Source: http://blog.csdn.net/u012637501 (embedded _ small J of the sky)I. static keywords1. static member variables/methodsWhen a static keyword is added before a class member (variable/method), this member is called a static member variable/method. Static variables are similar to global variables in other languages to some extent. If the static member variables are not private, they can be accessed outside the class, in this case, you do not need to generate an Instance Object of the class. You only need the class name (class name. can be referenced.2. static keyword usageVariables modified with static identifiers are created when the class is loaded. static variables exist as long as the class exists. Because static member variables can be shared by various instance objects, we can use them to achieve some special effects. (1) static code block: static {.......} A class can use a static code block that is not included in any method body. When the class is loaded, the static code block is executed and only executed sequentially, static blocks are often used for Class Attribute initialization. Source code: TestStatic. java

Class staticCode {static String school; static {school = ""; // The value of the static member variable is System. out. println ("I am staticCode") ;}} public class TestStatic {static {System. out. println ("TestStatic is loading... ");} public static void main (String [] args) {System. out. println ("In the main thread"); new staticCode ();}}

Print Information:

Analysis: Observe the information printed by the program. We can draw the following conclusions. A. the static code block in the staticCode class is automatically executed, even though two instance objects of the staticCode class are generated, however, the static code block is executed only once and only when the class is used will the static code block in the class be executed; B. the class is loaded only when the first one is used, instead of loading all classes that may be used in the program at startup. (2) single-State class design first, we understand the single-State Design Pattern of the next class, that is, to ensure that only one object instance can exist for a class in the entire software system, in addition, this class only provides one method to get its object instance. // Single-State class TestSingle {private TestSingle () {}// private constructor private final static TestSingle single = new TestSingle (); // construct a private TestSingle Instance Object public static TestSingle getSingleInstance () // return this private object {return single ;}}. Then, we can use TestSingle in the main thread. getSingleInstance () method to obtain the unique object of this single-State class. Source code: staticDemo. java
// Single-State class TestSingle {private TestSingle () {}// private constructor private final static TestSingle single = new TestSingle (); // construct a private TestSingle Instance Object public static TestSingle getSingleInstance () // return this private object {return single;} public void print () {System. out. println ("I am TestSingle class") ;}// main class public class staticDemo {public static void main (String [] args) {System. out. println ("In main thread"); TestSingle single = TestSingle. getSingleInstance (); // creates a single-State Class Object. print (); // call public methods in the single-State class }}


Print Information:

Analysis: to intelligently generate an object for a class in a virtual machine, we can use static to design a single-State Class. first, you must set the access permission of the class constructor to private, so that you can use the new operator to generate class objects outside the class, of course, we can still generate objects of this class within the class. B. in a single-State class, a new operator is used to generate an instance object. The instance object is static and private and can only be used by the static method of this class. because we cannot obtain the single-State class objects outside the class, we implement a static method in the single-State class to return the internally created objects. Note that static methods can only contain static member variables in the member class. 3. Considerations for using static keywords(1) In a static method, only other static members (including variables and methods) of the same type can be called directly, rather than non-static members in the category. For non-static methods and variables, you need to create an instance object for the class before use, and the static method does not need to create any objects before use. (2) static methods cannot reference the this and super keywords in any way. Because the static method does not need to create any instance objects before use, when the static method is called, the objects referenced by this are not generated at all. (3) The main () method is static and hides the Instance Object of the class where the main method is not created when the JVM executes the main method. Therefore, in the main () method, you cannot directly access non-static members in the class. You must create an Instance Object of the class before using this object to access non-static members in the class.
Ii. final keywordsWhen writing Java code, we often see that when declaring classes, attributes, and methods in Java, we use the keyword final to modify them. Next, let's take a look at the final keyword? 1. the classes marked by final cannot be inherited; 2. methods marked by final cannot be overwritten by the quilt class; 3. variables marked by final can be assigned only once, whether they are member variables or local variables. In addition, the member variables marked by final must be declared or displayed as values in the constructor of the class before they can be used. Class Test {final int x = 3 ;}or: class Test {final int x; Test () {x = 3 ;}} Note: (1) variables marked by final (member variables or local variables) become constants and can only be assigned values in sequence. However, this "constant" can only be used within this class, cannot be directly used outside the class. (2) define constants in Java. Generally, public static final is used to mark the constant, and then the constant becomes a global constant. However, constants defined in this way can only be assigned values at the time of definition, even in constructors, they cannot be assigned values.

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.