Java beginners to master-the static Keywords of the basic articles, the entry to master static
I. Overview
The static keyword declares static variables, which is used by static methods. Static means variables and functions that belong to a class and do not belong to class objects.
Ii. static generation.
When creating an object, unless the new class object is used to create the object, no objects are actually obtained. The data storage space is allocated only when the new class is executed to create the object, the method is for external calls. However, the above methods cannot be solved in two cases.
First, you only need to allocate a single bucket to a specific domain, instead of considering the number of objects to be created, or even creating any objects.
The second is to associate objects of a method (variable) with a class that does not contain it. This method can also be called if no object is created.
To solve the above two problems, the static keyword is born.
When a thing is static, it means that this domain or method will not be associated with any object instance of the class containing it. Even if you have never created any object of a class, you can call its Static method or its static field.
Iii. Example.
TestStatic. java
public class TestStatic {public static int i=33;}
StaticMain. java
public class StaticMain {/** * @param args */public static void main(String[] args) {System.out.println("TestStatic.i="+TestStatic.i);TestStatic s1=new TestStatic();TestStatic s2=new TestStatic();System.out.println("s1.i="+s1.i);System.out.println("s2.i="+s2.i);s2.i=133;System.out.println("s1.i="+s1.i);System.out.println("s2.i="+s2.i);}}
:
Iv. Summary.
In the example, we find that the first printed I value is that people who want a variable not to include its class are associated with objects. This method can also be called if no object is created. The following describes how to allocate a single bucket to a specific domain without considering the number of objects to be created or even creating any objects. The static keyword solves these two problems for us.
Static keywords need to be used with caution to avoid unnecessary troubles for the program.