1>static: Static modifier
Static represents the meaning of "global" or "static", which modifies member variables and member methods, and can also form statically static blocks of code, but there is no concept of global variables in the Java language.
member variables and member methods that are modified by static are independent of any object of the class. that is, it does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
static member variables and member methods that are decorated with public are essentially global variables and global methods, and when you declare objects of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable.
The static variable can have a private adornment before it, indicating that the variable can be used in the static code block of the class, or in other static member methods of the class (which can also be used in non-static member methods-nonsense), but it is important that the class name cannot be referenced directly in other classes. in fact, you need to understand thatprivate is the access permission limit, static means do not instantiate can be used , so it is easier to understand more. Static plus other access keyword effects and so on.
The static modified member variables and member methods are customarily referred to as static variables and static methods, which can be accessed directly through the class name, and Access syntax is:
Class name. static method Name (parameter list ...)
Class name. Static variable Name
A static code block is represented by a statically decorated block of code that executes when the Java Virtual Machine (JVM) loads the class (very useful).
1. Static variables
There are two ways to classify a class member variable statically: A variable that is statically modified, called a static variable or a class variable, and another variable that is not modified by static, called an instance variable.
The difference between the two is:
For static variables there is only one copy in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through objects (but this is not recommended).
For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).
2. Static method
Static methods can be called directly from the class name, and any instance can also be called, so a static method cannot use the This and Super keywords, and cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static). Only static member variables and member methods of the owning class can be accessed. Because instance members are associated with specific objects! This need to understand, want to understand the truth, not the memory!!!
Because the static method is independent of any instance, the static method must be implemented, not abstract.
3. Static code block
A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once. For example:
public class Test5 { private static int A; private int B; static { test5.a = 3; System.out.println (a); Test5 t = new Test5 (); T.f (); t.b = +; System.out.println (t.b); } static { test5.a = 4; System.out.println (a); } public static void Main (string[] args) { //TODO Auto Generate Method stub } static { test5.a = 5; System.out.println (a); } public void F () { System.out.println ("Hhahhahah");} }
Run Result: 3
Hhahhahah
1000
4
Static code blocks allow you to assign values to some static variables, and finally take a look at these examples, all in a static Main method, so that the JVM can be called directly without creating an instance when it runs the main method.
2>this: Represents the current object
First, this keyword has three main applications:
(1) This invokes a property in this class, that is, a member variable in a class;
(2) This invokes the other methods in this class;
(3) This invokes the other constructor methods in this class, which are placed on the first line of the constructor method when called.
Public Class Student { String name; Define a member variable name private void SetName (String name) { //define a parameter (local variable) name this.name=name; Pass the value of the local variable to the member variable }}
Apply one: Reference member variable
In general, referencing member variables or member methods in the Java language is the object name. A member variable or an object name. The form of a member method. Some programmers, however, prefer to use this as a member variable, even when they do not have the same variable, which is mainly from the convenience of code reading. As soon as you see this keyword, you know that the variable you are referencing is either a member variable or a member method, not a local variable. This virtually improves the readability of the code. But then again, this is the simplest application of the This keyword in the Java language. From this application, we can see that the This keyword represents the name of the object. In fact, if it is a local variable, it is the same reason. As in the above code, name is not a formal parameter, but a local variable. Java will also encounter the same doubts, that is, the variable name name is a local variable or formal parameters? What does name=name mean? According to the scope of the local variable, inside the method, if the local variable has the same name as the member variable, the local variable will prevail. However, in Name=name this assignment statement, it is obviously not appropriate to assign the value of the local variable to itself. According to the meaning of the code, the original meaning should be to assign a local variable to the member variable. In order to express this meaning more clearly, it is best to use the following writing format This.name=name. The This keyword here means the object name student, for which this.name represents Student.name.
Application Two: Constructing method of calling class
public class Student { //defines a class whose name is Student. Public Student () { ///define a method with the same name as the class so it is constructed by this ("hello!"), and the public Student (String name) {//Defines a constructor method with formal parameters}}
The This keyword can also call a constructor method, in addition to calling member variables. In a Java class, its methods can be divided into the member method and the construction method two kinds. A constructor method is a method that has the same name as a class, and a constructor method must exist in the Java class. If the constructor is not shown in the code, then the compiler will automatically add a constructor with no formal parameters when compiling. There are many different ways of constructing this method with ordinary member methods. If the constructor method has no return value, and the Void keyword is not used to indicate that the constructor does not return a value. A normal method can have a return value, or it can have no return value, and programmers can define it according to their own needs. However, if the normal method does not return a value, then be sure to use the Void keyword when the method definition is described. Second, the name of the construction method has strict requirements, that is, the name of the class must be the same. In other words, the Java compiler finds a method that is the same as the name of the class to treat it as a construction method. For common methods, the requirement cannot be the same as the name of the class, and multiple member methods cannot use the same name. There can be more than one construction method in a class that uses the same name, except for the formal parameter. The Java language determines the invocation of the constructor by the different form parameters.
In the above code, two construction methods are defined, one with parameters and the other without parameters. The constructor method does not have a return value, but because of the particularity of the construction method, it is not necessary to take the void keyword when constructing the method definition to illustrate the problem. In the first constructed method without parameters, this ("hello!") is used What does this code mean? In the constructor method, the This keyword represents the constructor method in the calling class. If there is more than one constructor in a class, because its name is the same and is consistent with the name of the class, what is the constructor called? In fact, this is the same as using other methods to refer to the constructor method, which is called by the formal parameter. As in the example above, the This keyword is followed by a parameter, which means that it refers to a constructor with parameters. If there are now three constructor methods, with no parameters, with one parameter, with two parameters. Then the Java compiler will determine which constructor to call based on the number of arguments passed. As you can see from the example above, the This keyword can be used not only to reference member variables, but also to reference construction methods.
However, if you want to invoke the constructor method in this way, there is a syntax limitation. In general, using the This keyword to invoke the constructor method, only the first sentence in the parameterless constructor method uses this to invoke the constructor with the parameter. Otherwise, when translating, there will be error messages. This is different from referencing member variables. If you refer to a member variable, the This keyword is not limited in position. If you are unfamiliar with this limitation, it is good to use the traditional method of constructor invocation. It's a hassle, but at least it doesn't go wrong.
Apply three: Return the value of an object
The This keyword, in addition to referencing variables or member methods, has a significant effect of returning a reference to a class. As in code, you can use return this to return a reference to a class. At this point, the This keyword represents the name of the class. If the code is in the student class above, then the code means return student. It can be seen that the this keyword, in addition to referencing variables or member methods, also serves as the return value of the class, which is the most noticeable part of this keyword.
The static and this of Java