Code reuse is a very important factor in software development, and it is a good idea to put common functions together and encapsulate them into a library. Whenever you need it, just call it. Creating a library like this is easy for Java to use. Here's a simple example of using Eclipse to create a math library with only an "add" method for the demo.
Step 1, create a Java Project named "MyMath", then create a class named "Simple" and add a static method "Add" to the class.
Package Structure:
Simple.java
1 Public class simple{2 Public Static int Add (int A,int b) {3 return a +b; 4 }5 }
Step 2, export to a jar file.
Right-click on the item and select "Export" to display the following window:
Click "Next" and follow the wizard to fill in the corresponding information, will generate a jar file, this is our library.
Step 3, use the library.
Right-click on the item that you want to use the library, and then choose Build Path, add External Archives, and then the wizard adds the library file to the project.
Now you can start calling functions in the library.
Main.java
1 Public class main{2 Public Static void Main (string[] args) {3 System.out.println (Simple.add); 4 }5 }
Finally, it is important to update and optimize the library frequently, and to write good documentation. If you don't write a good document for the library, you may have forgotten what the library is for. The name of the package to be able to see the name of the understanding, explain how these classes and methods are used. For example, you can name your first-tier package by imitating the standard Java library naming: Programcreek.util,programcreek.io,programcreek.math,programcreek.text, and so on. With a library of your specific domain knowledge can be exploited by others, in addition, before you start programming, you need to first search whether someone has achieved what you want to do, be good at using the industry's thousands of smart programmers power.
How to build your own Java library "translation"