A deep understanding of JAVA's static (combined with the C language) and a deep understanding of static

Source: Internet
Author: User

A deep understanding of JAVA's static (combined with the C language) and a deep understanding of static

1 public class statictest {
 2 
 3
 4 String X = "I am a non-static variable";
 5
 6
 7 static int butterfly = 0;
 8 static String staticX = "I am a static variable";
 9 static String staticZ = "I am also a static variable";
10
11
12 statictest () {
13 System.out.println ("I am the constructor");
14}
15
16
17
18 public static void main (String [] args) {
19 statictest this1 = new statictest (); // The new object is just for the following fangfa2 (), but it does n’t matter if you use the constructor in the static block
20 //System.out.println(ff3); wrong, indicating that the variable in the static method is also a temporary variable, not global
21 //System.out.println(staticY); wrong, indicating that even the quantity defined in the STATIC block is a temporary variable, not global, nor in this class
22 //this.fangfa2 (); wrong, indicating that this is an object and cannot be used in static methods
23 this1.fangfa2 ();
24 fangfa4 (); // I just want to explain that I do n’t need to use this1
25
26}
27
28
29 static {
30 statictest this2 = new statictest (); // Just to show that you can start a new object in the static block earlier, the purpose is to run the constructor
31 String staticY = "staticy";
32 //System.out.println(x); wrong
33 System.out.println (staticY);
34 //this.fangfa1 (); Wrong written like this, because this means that this object is an object value, and static is loaded before the object
35 fangfa1 (); // Describe that static blocks can call static methods
36 // if (true) {} Yes, it means that the static block can be regarded as a special static method that has no name and cannot be called. Its essence is a static method, which is also equivalent to the main method.
37 // run, and run before main
38}
39
40 static void fangfa0 () {
41 System.out.println ("I am a butterfly");
42} // This method just wants to show that I like butterflies, there is no specific use
43
44
45
46
47 static void fangfa1 () {
48 System.out.println (staticX);
49 //System.out.println(X); wrong, because static methods cannot call non-static quantities
50 System.out.println (fangfa3 ());
51 //System.out.println(ff3); wrong, it means that ff3 is defined as a static method, but it is still a temporary variable, but if you add static in front of it, it will not be wrong
52
53
54}
55
56 static String fangfa3 () {
57 String ff3 = "I am fangfa3";
58 return ff3;
59
60}
61
62 static void fangfa4 () {
63 fangfa0 ();
64}
65
66
67 void fangfa2 () {
68 System.out.println (staticZ); // Static methods can call static quantities
69 this.X = "2";
70}
71
72
73
74}
75 / * Print result: * /
76 // I am a constructor
77 // staticy
78 // I am a static variable
79 // I am fangfa3
80 // I am a constructor
81 // I am also a static variable
82 // I am a butterfly
83
84
85 / * Summary process: 1. Static loading and initialization: Initialize the static variables (variables and methods). The initialization of the method means that the variable name is assigned to the variable name; the initialization of the variable is
86 * Assign a general data type directly to this variable
87 * 2, static execution: Then execute the content in the static block, which is equivalent to the main method is executed first, is the static execution entry is like "main",
88 * 3, non-static loading and initialization: it is the turn of all non-static variables and methods of loading and its assignment
89 * 4, execution constructor
90 * 5, non-static execution: execute the main method
91 * Note: The method assignment and call are two concepts. The method assignment is to assign the address pointer to the method name, which is the indirect variable; the method call is to call the method
92 * Note: There are non-static blocks not mentioned
93 * Note: The three concepts of execution and loading and initialization must be clearly separated
94 * Note: static is common to all objects of this type, that is, one is only one, and the non-static amount is that each object has its own, like the newly created variable X, this2.X and this1. X is not
95 * same
96 * Note: Because the static amount is shared, it is better not to add the object name and call it directly, which is helpful for the understanding of the concept.
97 * /


 


Java static functions

I want to explain it in detail. Could you find me some information?

Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables.

The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class.

As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.

Static member variables and member Methods Modified with public are essentially global variables and global methods. When the object city of the declared class does not generate a copy of static variables, instead, all instances of the class share the same static variable.

The static variable can be modified in private, indicating that the variable can be in the static code block of the class, or other static member methods of the class (you can also use -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much easier to understand. The effect of adding other access key words before static is also similar.

Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is as follows:
Class Name. Static Method Name (parameter list ...)
Class Name. static variable name

A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed (very useful ).

1. static variables
Class member variables can be classified by static or not. One is static modified variables, such as static variables or class variables; the other is a variable that is not modified by static, called an instance variable.

The difference between the two is:
For static variables that only have one copy (memory saving) in the memory, JVM only allocates the memory for the static one time, and completes the memory allocation of the static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ).
If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).

Therefore, static variables are generally used to implement the following functions:
When a shard shares a value between objects
When the ingress facilitates variable access

2. Static Method
Static methods can be called directly by class names, and can be called by any instance,
Therefore, the keyword "this" and "super" cannot be used in static methods, and the instance variables and instance methods of the class cannot be directly accessed (that is, the static member variables and Member member methods are not included ), only static member variables and member methods of the class can be accessed.
Because instance members are associated with specific objects! You need to understand the truth, not the memory !!!
Because the static method is independent of any instance, the static method must be implemented rather than abstract.

For example, to facilitate method calling, all the methods in the Math class in Java API are static, while the static method in the general class is also convenient for other classes to call this method.

Static methods are a special class of internal methods. They are declared as static only when needed. Internal methods of a class are generally non-static.

3. static code block

A static code block is also called a static code block. It is a static statement Block Independent of class members in a class. It can have multiple static code blocks and can be placed anywhere. It is not in any method body, when a JVM loads a class, it executes these static code blocks. If there are multiple static code blocks, the JVM executes them in sequence according to the sequence they appear in the class, and each code block is executed only once. For example:

Public class Test5 {
Private static I... the remaining full text>

Static is different in C and Java.

Static declared variables have two features in the C language:
1) The variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.
Tips:
A. if the global variable is only accessed in A single C file, you can change the variable to A static global variable to reduce the coupling between modules;
B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;
C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you must consider re-import;
D. If we need a reentrant function, we must avoid using static variables in the function (such a function is called a function with the "internal memory" function)
E. static variables must be used in a function. For example, if the return value of a function is of the pointer type, the address of the static local variable must be used as the return value. If the return value is of the auto type, the returned result is an error pointer.
Add static before the function to make the function a static function. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file (so it is also called an internal function ). The advantage of using internal functions is that when different people write different functions, you don't have to worry about your own defined functions and whether they will have the same name as the functions in other files.
Extended analysis: the term static has an unusual history. At first, the keyword static was introduced in C to indicate that a local variable still exists after exiting a block. Then, static
C has a second meaning: it is used to indicate global variables and functions that cannot be accessed by other files. To avoid introducing new keywords, the static keyword is still used to indicate the second meaning. Finally,
C ++ reused this keyword and gave it a third meaning different from the previous one: represents the variables and functions that belong to a class rather than any specific objects of this class (which is consistent with the meaning of this keyword in Java
).

What is the role of the static keyword?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1. In the function body, a variable declared as static remains unchanged when the function is called.
2. Within the module (but in the external body of the function), a variable declared as static can be accessed by the function used in the module, but cannot be accessed by other functions outside the module. It is a local global variable.
3. In a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most of the respondents can answer part 1 correctly. Some of them can answer part 2 correctly. Few people can understand part 3. This is a serious disadvantage of the candidate, because he obviously does not understand the benefits and importance of localized data and code scope.

Int testStatic ()
{
Int x = 1;
X ++;
Return x;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d \ n", testStatic ());
Getch ();
}

= "2 2 2 2 2 2

Int testStatic ()
{
Static int x = 1;
X ++;
Return x;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d \ n", testStatic ());
Getch ();
}

= "2 3 4 5 6

The reprinted source address is
... The remaining full text>

Related Article

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.