How to build your own Java library "translation"

Source: Internet
Author: User

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"

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.