creating your first package

Creating your own Java Package in Eclipse:

Generally, if we have 2 or more related classes, then we package these into a class library(in Java, class libraries are called packages). So, just how do we create our own package in Eclipse?

1. Click on File, then New, then Package and that will bring up the following dialog box:

 

package

2. In the Name field above enter a name for your package, I have called mine MySwingClasses above, but you can call it whatever you like. Make sure the Source Folder is pointing to the source(src) f older of your project : for example, FirstProject/src.Then just click finish.

3. Now if you click on your project in the Package Explorer window in Eclipse, you will see your new package displayed, as shown below:

Fig 1.1 Package Explorer showing new package
Fig 1.1 Package Explorer showing new package

4. Finally We need to copy our existing class into our new package. In the last post we created MyClass.java which is stored in the default package. just click on the default package and you will see MyClass.java. Just right click on MyClass.java and then choose “Cut”, and then click on the new package, and click paste. MyClass.java will now be stored in your new package. Finally, delete the default package altogether, by highlighting it again. Right clicking on it and choosing Delete. When you are finished, it should look something like below.

package3

Congratulations, you just created your first Java Package! But there is still one more surprise! If you open up MyClass.java you will see the following line included at the top of the file.

—————————————————–
package MySwingClasses
—————————————————–

 

Leave a comment