0

I have a first project has a feature to viewing, inserting and updating an employee record, with this possibly I will create a java class for the UI and another for the database connection with commands for viewing, inserting and updating a record.

Then I have a second project that will compute the salary of employee. How can I re-use object from the first project?

Should I get the Java or the class file or both of the first project and saved it to the project folder of the second project? How can add a feature of object from the first class?

1
  • 4
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Apr 16, 2014 at 6:36

2 Answers 2

1

The artifacts (i.e. class files, resource files, etc.) from the first project should be on the classpath of the second project.

If your project is quite small, you can specify the location to project one's class files on the command line or with environment variables. See http://docs.oracle.com/javase/tutorial/essential/environment/paths.html.

Otherwise, you should look into some project management or build software, like maven or apache ant.

0

There're several ways to do that:

  • Copy the .java file into your project files. This has the disadvantage that you will have two source files.

  • Add the path to the .class file to your project's classpath.

  • Create a .jar file and add the path to the .jar file to your project's classpath. Note that the path includes the jar file itself, as if it were a folder. This make it cleaner when you want to share more than just one class, and it's the standard way of distributing libraries.

  • If you are using Eclipse, you can tell eclipse to add OldProject to NewProject's built path. Then you can import OldProject classes in your classes with the import reserverd word. Also when creating NewProject's jar for distribution, Eclipse will offer you the option of including OldProject classes in the bundle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.