Use static inner classes to add accessibility to your code

Source: Internet
Author: User
Tags definition static class domain
Static
Use static inner classes to add accessibility to your code author John D. Mitchell



This tip provides an example that allows you to add independent, optional accessibility features, such as Tests and sample code, to your class using static inner classes. (500 words)



With this technique, you can add the use of static internal classes to your Java usage tips. A static inner class is a class that is defined in the definition of another class and is marked as static. I'm going to show you an example that uses static inner classes to add test code to another class.



Static inner classes are simple in concept and implementation, basically defining a static class in your main 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 ();







}



}



}



When it comes to adding secondary code to your main class, the most important thing is that the static inner class is compiled into a separate. class file that is independent of its external class. For example, if the outer class is called Foo, and one of its internal classes is called Test, then the inner class is compiled into a foo$test.class file. The separation of the. class file means that you can securely bundle the auxiliary nested code with the main external classes. They are in the same source file, and the inner class is indeed inside the outer class. You no longer have to pay any release or runtime overhead. That's great! For example, if the secondary code is for debugging only, you can simply publish the Foo.class file and leave the Foo$test.class file.



I use this technique primarily to write demo code for external classes, debug code incorrectly, and automate validation of class behavior with unit tests. (Of course, as a diligent developer, I'm going to convert the test code into a unit test.) )



Note that to perform the main () method of the Testdebug.class class, use the following command:



% Java testdebug$test



If you are using the command interpreter (shell) to make "$" as a reserved word, you should use the following command:



% java testdebug\ $Test



10 points interesting: Static inner classes can access the protected domain and private domain of the external class according to the definition. This can be said to have both advantages and disadvantages. Because you may inadvertently break the protection domain and private domain of the external class, thus violating its encapsulation, so please be careful! The most appropriate application of this function is to write a white-box test program for the class--because it introduces some problems that are difficult to introduce using the usual black-box tests (Black-box tests cannot access the internal state of an object).



Conclusion by using static internal classes, you can add accessibility features to your system to complete work such as testing, without any adverse impact on the products that are officially released.








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.