Global variables in Java

Source: Internet
Author: User

I am using netbeans to build a program that is a little larger. I just learned how to use it. I feel that global variables are needed in the Program (in fact, there is no concept of global variables in Java ).
I checked a lot of information on the Internet and got a little bit of experience:
First, Java does not have the concept of global variables. Java programs cannot define global variables outside the class as C ++ does, because Java was originally designed for security and cross-platform, it removed the concept of global variables similar to C and C ++. In Java, the so-called global variables should not exist. global variables seriously affect encapsulation and modularization. Therefore, the so-called global variables must be a problem in programming. The above is my opinion on the Internet, but for now, a variable that can be used globally is necessary.
Second, Java provides the public static keyword to implement a global variable. If the program only has one package, it will be public static (variables declared as static can be referenced directly by class names without instantiating objects) the modified variable is implemented in the place where the program is initialized, so that the variable can be directly called in each place of the program. Although this is not a global variable, it actually means the same as the global variable. Of course, you can also completely ignore global variables. You can write a class, define general variables, and define some methods to operate variables for the class, you can call these methods to change variables to use global variables.
Also, for static usage, static can modify variables, methods, and classes, but static can only be internal classes.
Static internal classes are very simple in terms of concept and implementation. Basically, it is to define a static class in your main class:
Public class foo
{
//....

Public static class test
{
Public static void main (string [] ARGs)
{
//....
}
}
}

When it comes to adding auxiliary code to your main class, the most important thing is that the static internal class is compiled into a separate. Class file, which is independent of its external class. For example, if an external class is foo and one of its internal classes is test, the internal class will be compiled into the foo $ test. Class file .. The separation of class files means that you can firmly bind the auxiliary nested code with the main external class. They are in the same source file, and the internal class is indeed inside the external class. You do not have to pay any post or runtime expenses. Awesome! For example, if the auxiliary code is only used for debugging, you only need to release the foo. Class file and leave the foo $ test. Class file.
I mainly use this technique to compile DEMO code for external classes, debug code for errors, and perform unit tests to implement automatic verification of class behavior. (Of course, as a diligent developer, I plan to convert the test code into a unit test .)
Note: To execute the main () method of the foo. Test class, use the following command:
% Java Foo $ Test
If the command interpreter (Shell) You are using uses "$" as a reserved word, you should use the following command:
% Java Foo // $ Test
Also interesting: static internal classes can access the protection domains and private domains of external classes according to definitions. This can be said to have both advantages and disadvantages. Because you may inadvertently destroy the protection domain and private domain of the external class, thus violating its encapsulation, please be careful! The most appropriate application for this function is to compile a white-box testing program of the Class-because it can introduce some problems that are difficult to introduce using the general black box testing (black box testing cannot access the internal state of the object).
The xypair class is very simple. It provides a fixed integer pair (x, y ). The xypair. Test class has a main () method to perform simple tests on xypair and output the results. Try to adjust the test code and core code to test various possible problems.
If you are more bold, you may want to test the Java unit test architecture (JUnit ). You can remove comments from the source code and use the JUnit test engine to run these tests.

Note:
Static member functions or static variables defined by static can be called directly by the class name to which they belong. Why? Since all objects in this class use this variable, I naturally don't need to reference it from any of them, but just use the class name to reference it. isn't this easy to implement some global functions and global variables? Define all global functions or global variables in a static class. You can use this class name to conveniently access all global variables and global functions.

Appendix:
It is true that global variables may have practical significance in some environments. However, in Java, there is indeed no concept of global variables. By setting an abstract class or interface, many final or final static fields are placed in it, and classname is called during use. xxx or interfacename. xxx to simulate the use of global variables (it is certain that, in many books, masters have repeatedly stressed putting many constants into an abstract class or interface, the practice of making it a constant class or constant interface is misuse of this function and does not encourage such use. If you are interested, please refer to "valid Java"-published by Joshua Bloch by Mechanical Industry Press),
First, final or final static is indeed not a global variable. in Java, everything is an object, the field, method, or property declared in the object will belong to an abstract or specific type. Otherwise, classname will not be used in the call. xxx or interfacename. xxx indicates that this is the XXX of the class, that is the XXX of the interface. In fact, final represents a constant form (constant), while static represents a static concept. A constant is a constant variable that exists to distinguish it from a variable, suddenly found that their words are poor, Hoho), while static is to be different from a specific object and exists independently in a specific type of variable (which can be called a global variable of this type, but I personally don't feel very specific ). The concept of global variables is obviously too broad, so that we can say that a program or even a system has a unique variable and becomes possible, but final or static is obviously not designed for it (of course it can be simulated ).
Secondly, you mentioned that "there is only one full virtual machine, and the database connection pool object..." is the design model.
The actual application of the so-called Singleton mode in formula. This mode is indeed very similar to the so-called global variable concept, however, the design of such a single instance is indeed because only one instance is needed throughout the life cycle of the system. More is to highlight concepts rather than practical applications, global variables are generated for practical use, which leads to many immature, apps without thinking are added to it, resulting in a paste of code like pasta.
Therefore, I personally think that I am not sure to comment out whether global variables are real or not.
This is a huge topic. Leave it to those experts for discussion. As for how to actually apply global variables, I think there are still some ways to do this. If there is no such thing, we can do it even. (However, since using C ++/Java, the use of global variables has indeed dropped to a very low level, maybe because there are still too few codes to be written, huh, huh ...). (from http // topic.csdn.net/t/20050112/22/4109146.html)

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.