How to create packages in Java?

Posted by Joys of Programming on in Java

Creating packages in java is more than writing a single line in your java file


package packageName;

There are certain things to jot down in your mind before creating a package. A package name has the following format.


string(.string)*

It signifies that a package name consists of one or more strings separated by a period (.). Take for example


tutorials

tutorials.wmessage

tutorials.sutils

tutorials.wmessage.translate

But to use a package, the above definition will not suffice. The modified form is this


dir(.subdir)*

which means that a package name corresponds to the relative path of a Class. Here dir corresponds to the main directory of the package you are creating and subdir correspond to the subdirectories within that direcrtory.

Let’s explore this more. If you write the following line in Tutorial.java


package tutorial;

It is mandatory that the file Tutorial.java is defined in tutorial/Tutorial.java.

Similarly a line in a java class


package tutorial.sutils;

means that it is defined in tutorial/sutils.

Let’s explore more on this with example. I am going to create two classes WelcomeString.java and StringUtils.java


$ mkdir  tutorials

$ mkdir tutorials/sutils

$ mkdir tutorials/wmessage

$ touch tutorials/sutils/StringUtils.java #creating a blank java file

$ touch tutorials/wmessage/WelcomeMessage.java #creating a blank java file

Let’s see the file tutorials/sutils/StringUtils.java


package tutorials.sutils;

/** Set of Operations related to String
 */
public class StringUtils {

 /*Concatenates two strings*/
 public static String stringConcatenate (String str1, String str2) {
 return str1+str2;
 }

}

Note the line


package tutorials.sutils;

It is defined in tutorials/sutils, hence the name.

Let’s define tutorials/wmessage/WelcomeMessage.java


package tutorials.wmessage;

/** To deal with  Welcome Messages
 */
public class WelcomeMessage {

 /* Default Message prepended before any welcome message*/
 String message = "Welcome to the world of Java programming\n";

 /*Uses the default Welcome Message*/
 public WelcomeMessage() {
 }

 /*Prepends the default welcome message to the user specified message*/
 public WelcomeMessage (String message){
 this.message += message; /*Concatenation of the two message*/
 }

 /*Prints the Welcome message*/
 public void print(){
 System.out.println(message);
 }

 /*Returns the Welcome Message*/
 public String welcomeMessageString(){
 return message;
 }
}

See the first line


package tutorials.wmessage;


As mentioned above, it is defined in tutorials/wmessage, so the package name tutorials.wmessage.

Note that all the files in a directory will have the same name.


Tags: , , , , , , , ,

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.