Java Basics-Learning Note (ix)--static keywords

Source: Internet
Author: User

1. Static member variables

The class in the program only describes the properties and members of its object, and does not produce the actual object, only through the new keyword to produce the object, then the system will allocate memory space to the object, its method can be called externally.

Each generation of an object will be allocated a separate space in memory, and sometimes we hope that no matter how many objects are generated, some specific data in the memory space only one, we just need to add the Statice keyword in front of this data. Static variables are somewhat similar to global variables in other languages. Also, we can invoke the class name without having to produce an instance object, as shown in the following code

1 classChinese2 {3     StaticString country= "China";4 String name;5     intAge ;6     voidsingourcountry ()7     {8SYSTEM.OUT.PRINTLN ("Ah! , dear "+country);9     }Ten      One } A classTestchinese - { -      Public Static voidmain (String [] args) the     { -SYSTEM.OUT.PRINTLN ("China is" +chinese.country); -Chinese ch=NewChinese (); - Ch. Singourcountry (); +     } - } + /* A China is Australia at Ah! , dear China*/
View Code

2. Static method

Sometimes you do not want to create an object to invoke the method, that is, you do not want the method to be bound together with the object, to achieve this, we need to add the static keyword before the method defined by the class, which can be called a static member method. As with static member variables, it can also be called directly by the class or by creating an object.

1 classChinese2 {3 String name;4     intAge=5;5     Static voidsingourcountry ()6     {7         //System.out.println ("My Age is" +age);8System.out.println ("Ah, dear Motherland!"));9     }Ten     voidSing ()//non-static methods in the same class can call static methods One     { A chinese.singourcountry (); -     } - } the classTestchinese - { -      Public Static voidmain (String [] args) -     { + chinese.singourcountry (); -Chinese ch=NewChinese (); + Ch. Singourcountry (); A Ch. Sing (); at     } - } - /* -  - F:\java_example>java Testchinese - Ah, dear motherland! in Ah, dear motherland! - Ah, dear motherland!*/
View Code

Precautions:

1) Static member methods cannot access other non-static variables or non-static member methods in the class. Because there are no objects created at this time, non-static member variables and non-static member methods have no space allocated, they do not exist;

2) The This and super keywords cannot be referenced in any way in a static method. Ditto, because there is no object created, so I do not know which object this is pointing to;

3) since main () is a static method, he cannot access non-static members in the class and need to create a new instance object before it can be referenced.

3. Static code block

A class can use a static block of code that is not contained in any method, and when the class is loaded, a static block of code is executed and executed once, and static code blocks are often used to initialize class properties.

1 classStaticcode2 {3     Static intAge ;4     Static5     {6Age=23;7System.out.println ("Staticcode is loading ...");8     }9 }Ten classTeststaticcode One { A     Static  -     { -System.out.println ("Teststaticcode is loading ..."); the          -     } -      Public Static voidmain (String [] args) -     { +SYSTEM.OUT.PRINTLN ("Begain executing Main method ..."); -         NewStaticcode (); +         NewStaticcode (); A     } at } - /* - F:\java_example>java Teststaticcode - teststaticcode is loading ... - begain Executing Main method ... - staticcode is loading ...*/
View Code

The above code fully illustrates that the static block is run when the class is loaded and executes only once.

4. Single State design mode

The singleton means that the class only creates one instance object

1 classStaticcode2 {3     PrivateStaticcode ()4     {};5     StaticStaticcode obj =NewStaticcode ();6      Public Staticstaticcode getobj ()7     {8         returnobj;9     }Ten } One classTeststaticcode A { -      Public Static voidmain (String [] args) -     { theStaticcode obj1=staticcode.getobj (); -Staticcode obj2=staticcode.getobj (); -         if(obj1==obj2) -System.out.println ("Only one instance~"); +     } - } + /* A F:\java_example>java Teststaticcode at Only one instance~*/
View Code

First of all, we want to restrict the class to produce only one object in the program, we know that the object is generated through the constructor function, then we start from here, restrict the constructor, do not let the external call our constructor, it needs to be decorated with private;

Again, we have said that there can only be one object, and since we cannot call the constructor outside, we have to generate an object within the class and provide a way for the external program to access it;

Also, how can we call our method to get the only object instance if there is no external object? Then we think of the natural way, this method into a static method, without the external creation of objects can be used;

Finally, why add a static when creating an object instance? If not added, we use obj in the static method, which is a non-static member that cannot be accessed for use.

Java Basics-Learning Note (ix)--static keywords

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.