Adding auxiliary function by using static internal classes for your code Author John D. Mitchell
Summary This tip provides an example, which can use a static internal class to add separate, optional auxiliary functions such as testing and sample code, such as test and sample code. (500 words)
Learn this skill, you can join the use of static internal classes to your Java usage skills collection. Static internal classes are defined in the definition of another class and labeled a static class. I will show you an instance that uses a static internal class to add test code to another class.
Static internal classes are very simple in concept and implementation. Basically, it is to define a static class in your primary class:
Public Class Testdebug
{
Private Double Num;
Public Testdebug (double in)
{
Num = in;
}
Public void output ()
{
System.out.println (NUM);
}
Public Static Class Test
{
Public static void main (string [] args)
{
Testdebug TD = New Testdebug (3.9);
Td.output ();
}
}
}
It is said to add auxiliary code to your major classes, the most important point is that the static internal classes are compiled into a separate .class file, this file is independent of its external class. For example, if the external class is called Foo, and an internal class called Test, then this internal class will be compiled into the Foo $ Test.class file. The separation of the .CLASS file means you can bundle the auxiliary nested code with the main external classes. In the same source file, the internal class is indeed inside the external class. You don't have to pay any release or running overhead. Awesome! For example, if the auxiliary code is just used for debugging, you only need to post the foo.class file and leave the Foo $ Test.class file.
I use this technique mainly to prepare the external class's demo code, error debug code, and automatic authentication of the unit test implementation class behavior. (Of course, as a diligent developer, I am going to convert the test code into unit testing.)
Note that you want to execute the main () method of the TestdeBug.class class, use the following command:
% Java Testdebug $ TEST
If you are using the command interpreter (shell) as a reserved word, you should use the following command:
% Java Testdebug / $ TEST
There is also a little interesting: static internal classes can access the external class and private domains depending on the definition. This thing can be said that it is both advantageous. Because you may undermine the protection domains and private domains of the external classes in unwittime, so that it is careful! The most appropriate application of this feature is to prepare a class of white box test procedures - because this can introduce some problems that use the usual black box test (black box testing that cannot access the internal state of the object).
Conclusion By using static internal classes, you can add an auxiliary feature to your system to complete work such as testing, and products that are officially released will not have any adverse effects.