Java under static keyword usage

Source: Internet
Author: User

Java under static keyword usage
This article describes the use of the static keyword under Java, most of it is from the original author, here to learn and share to everyone.
What can the static keyword modify?
As can be seen from the following tests, static can be modified:
1. Statement block
2. member variables (but cannot modify local variables)
3. Methods
4. Interface (internal interface)
5. Class (can only be decorated in classes, i.e. static inner classes)
6. New static import in JDK 1.5
So what does the static modifier mean? When a class is created, it is creating a new type that describes the appearance and behavior of the object of the class, unless the object of that class is created with new, the data storage space is allocated and its methods are called by the outside world. So when declaring a thing as static, it means that the field or method is not associated with any object of the class that contains it, that is, it does not depend on a class-specific instance and is shared by all instances of the class, as long as the class is loaded, Java virtual machines can find them in the method area of the runtime data area based on the class name. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
1. Static modifier statement BLOCK:
static{} can have multiple places, and the order in which they are executed is in their order of declaration. When the JVM loads this class, it executes this block of statements and executes only once.
2. Static variable:
A static variable, which can be shared by all objects of the class, and it has only one copy of the storage space, the JVM allocates only one memory at a time and completes the memory allocation of the static variable during the loading of the class. The use of (class name. Variable name) is the preferred way to refer to a static variable, because he not only emphasizes that it is a static structure, but in some cases provides a better opportunity for the compiler to optimize.
It is relatively recommend to refer to a static variable directly using the class name, so it is clear that the variable or property is static or static.
But I want to say a more important point of knowledge. Static in the class when the important use of variables, first the landlord first run the following code to try:
public class Person {
Static int[] arr = new INT[3];
public static void Main (string[] args) {
Person per1 = new person ();
Person per2 = new person ();
Person Per3 = new person ();
SYSTEM.OUT.PRINTLN ("-----Before----change");
System.out.print ("per1-->");
Per1.show ();
System.out.print ("per2-->");
Per2.show ();
System.out.print ("per3-->");
Per3.show ();
Now I change the value of it
person.arr[2]=10;//This method is recommended
per1.arr[0]=1;//generally do not use this method
SYSTEM.OUT.PRINTLN ("-----After----change");
System.out.print ("per1-->");
Per1.show ();
System.out.print ("per2-->");
Per2.show ();
System.out.print ("per3-->");
Per3.show ();
}
For convenience, encapsulate a method to explain
public void Show () {
for (int i=0; i
System.out.print (Arr[i] + "T");
}
System.out.println ("");
}
}output:
Before----Change-----
per1-->0t0t0t
per2-->0t0t0t
per3-->0t0t0t
After----change-----
per1-->1t0t10t
per2-->1t0t10t
per3-->1t0t10t
Similar forms apply to methods that use the static modifier (called a static method). It can be referenced in the same way as a normal method, or it can be invoked in a special syntax form such as "class name" + ". "+" Method "(); Static methods are defined in the same way as static variables:
public class StaticTest2 {
static void Method () {("This is a static approach");}
}
Normal invocation method: StaticTest2nd = new StaticTest2 (); Because method is static, it can be called: StaticTest2. ()。
3. Static method:
The difference between a static method and a non-static method is that the static method can directly access variables and methods inside static with the class name, not a variable or method that is not static, but you can pass a reference by passing a parameter. Let this reference invoke non-static data.
4. Static interface:
The internal interface (inner interface) itself is static by default, so the static keyword plus does not add the same, so the interface in this example: Testininterface,testinclass implement this interface, the following statement:
Staticdescription.testininterface a = new Staticdescription.testinclass ();
A.print ();
We remove the static keyword as well, but are still subject to access control TestInterface and ff.
5. Static modifier class:TOEFL Answers www.daan678.com
Indicates that it is a static inner class, which must also be static when referencing something outside the class. Also, static inner classes do not require the presence of a perimeter class when creating their own objects. It's like the following section:
Class Member {
static int classvar = 0; class variables
int Instancevar; Instance variable
Member (int instancevar) {
This.instancevar = Instancevar;
}
static void Setclassvar (int i) {
Classvar=i;
Instancevar=i; Class methods cannot access instance variables, only class variables
}
static int Getclassvar () {//class method
return Classvar;
}
void Setinstancevar (int i) {
Classvar=i; Instance methods can access not only class variables, but also instance variables
Instancevar=i;
}
int Getinstancevar () {
return Instancevar;
}
}
Member not instantiated: after member instantiation:
1
For non-static data members, each class object has its own copy. Static data members are treated as members of the class. Regardless of how many objects of this class are defined, static data members have only one copy in the program and are shared by all objects of that type. In other words, a static data member is common to all objects of that class. For multiple objects of this class, static data members are allocated only once for all objects to be shared. Therefore, the value of the static data member is the same for each object, and its value can be updated;
Member m1 = new Member ();
Member m2 = new Member ();
2
There are two ways to reference a static variable. As shown in the preceding example, it can be positioned by an object, such as M1.classvar; It can also be directly referenced by its class name, such as Member.classvar, but not for non-static members.
Although static works on a field, it certainly alters how the data is created (because a static field has only one copy of the storage space for each class, and not a static field has a storage space for each object), but if static is used for a method, The difference is not that big. An important use of the static method is that it can be called without creating any objects. This is important for defining the main () method, which is the entry point when running an application.
The static method is the method without this. A non-static method cannot be called inside the static method, which in turn is possible. It is also possible to invoke the static method only through the class itself, without creating any objects. This is actually the primary use of the static method. It's much like a global approach. The global method is forbidden in Java, but you can access other static methods and static fields by placing the static method in the class.Sat Answer www.yzyxedu.com
As with any other method, the static method can create or use the same named object as its type, so the static method usually takes the role of "shepherd" and is responsible for caring for the same type of instance group as the slave.
However, if you have a large number of static methods in your code, you should reconsider your design.Act Answers www.sats686.com

Java under static keyword usage

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.